我想使用 android xml 布局。我在框架布局中放置了一个 glSurfaceView,以便与这样的线性布局结合使用...
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.opengl.GLSurfaceView android:id="@+id/surfaceviewclass"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<LinearLayout android:id="@+id/gamecontrolslayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
android:background="@drawable/backdrop">
//some layout stuff
</LinearLayout>
<LinearLayout>
然后我这样调用我的布局
setContentView(R.layout.main);
GLSurfaceView glSurfaceView = (GLSurfaceView)findViewById(R.id.surfaceviewclass);
在onCreate()中;
如何调用我的 glSurfaceView 以便我可以像这样使用 xml 布局并引用我自己的 GLSurfaceView 类(下面是引用我自己的 GLSurfaceView 类的代码)...
glSurfaceView = new MyGLSurfaceView(this);
setContentView(glSurfaceView);
有没有办法把这两者结合起来?我想这样做是因为我的 glSurfaceView 类中有很多事情要做,比如文件加载和触摸事件。只有我刚刚考虑过实现这个新布局
最佳答案
只需在 xml 中引用您自己的类(具有完整的包名),就像您引用 android.opengl.GLSurfaceView 一样。确保您的子类实现了正确的构造函数,并将上下文和属性传递给父类:
public MyGLSurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
然后您可以使用 findViewById 获取它:
MySurfaceView glSurfaceView =
(MySurfaceView)findViewById(R.id.surfaceviewclass);
这应该可以解决问题。
关于java - 将 GLSurfaceView 类与 android xml 布局一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7905995/