Android 设置glsurfaceview的高度
我想用自定义高度设置 glsurfaceview 的高度。 我希望宽度与高度相同。 使用 android:layout_width="fill_parent"。 我该如何让 width = height = width_of_the_screen ?
多谢
I want to set the Height of glsurfaceview with custom height.
I want that the width is the same of height.
With android:layout_width="fill_parent".
How I do to have width = height = width_of_the_screen ?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要重写 onMeasure 并将 setMeasuredDimension 的两个参数设置为接收到的宽度,如下所示:
布局如下:
You need to override the onMeasure and set both parameters of setMeasuredDimension to the received width as seen below:
with a layout as follows:
您可以使用
view.getLayoutParams().height = view.getMeasuredWidth();
以编程方式设置高度。问题是这必须在第一次绘制视图后完成,否则 < code>measuredWidth 只会返回零。 (假设您使用的是 openGL),您可以相当确定,当调用 Renderer 类中的onDraw
时,它肯定会绘制在屏幕上。但是,您必须在 GL 线程(调用onDraw
)和(主)UI 线程之间传递消息,这是唯一允许更改视图的宽度/高度等的线程You can set the height programatically by using
view.getLayoutParams().height = view.getMeasuredWidth();
The problem is this has to be done after the view has been drawn for the first time, otherwisemeasuredWidth
will just return zero. (Assuming you're using openGL), you can be fairly sure that by the timeonDraw
in your Renderer class is called, it is definitely drawn on screen. However, you'd then have to communicate a message between the GL Thread (that callsonDraw
) and the (main) UI thread, which is the only one allowed to change views' width/height etc