我想用自定义高度设置 glsurfaceview 的高度。我希望宽度与高度相同。使用 android:layout_width="fill_parent"。我如何才能拥有 width = height = width_of_the_screen ?
非常感谢
我想用自定义高度设置 glsurfaceview 的高度。我希望宽度与高度相同。使用 android:layout_width="fill_parent"。我如何才能拥有 width = height = width_of_the_screen ?
非常感谢
您需要覆盖 onMeasure 并将 setMeasuredDimension 的两个参数设置为接收到的宽度,如下所示:
class TouchSurfaceView extends GLSurfaceView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = View.MeasureSpec.getSize(widthMeasureSpec);
this.setMeasuredDimension(width, width);
}
...
布局如下:
...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="top"
>
<se.company.test.TouchSurfaceView android:id="@+id/glSurface"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
...
您可以通过使用以编程方式设置高度view.getLayoutParams().height = view.getMeasuredWidth();问题是这必须在第一次绘制视图后完成,否则measuredWidth只会返回零。(假设您使用的是 openGL),您可以相当确定onDraw在调用 Renderer 类时,它肯定是在屏幕上绘制的。但是,您必须在 GL 线程(调用onDraw)和(主)UI 线程之间传递消息,这是唯一允许更改视图宽度/高度等的线程