使用带有android xml布局的GLSurfaceView类

Jac*_*ack 13 java android opengl-es glsurfaceview android-layout

我想利用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>
Run Code Online (Sandbox Code Playgroud)

然后我就像这样调用我的布局

setContentView(R.layout.main);
    GLSurfaceView glSurfaceView = (GLSurfaceView)findViewById(R.id.surfaceviewclass);
Run Code Online (Sandbox Code Playgroud)

在onCreate();

我怎样才能调用我的glSurfaceView以便我可以使用这样的xml布局并引用我自己的GLSurfaceView类(下面是引用我自己的GLSurfaceView类的代码)...

glSurfaceView = new MyGLSurfaceView(this);
    setContentView(glSurfaceView);
Run Code Online (Sandbox Code Playgroud)

无论如何将这两者结合起来?我想这样做因为我在我的glSurfaceView类中有很多东西,比如文件加载和触摸事件.而且我只想到实现这个新布局

svd*_*ree 21

只需在xml中引用自己的类(带有完整的packagename),就像引用android.opengl.GLSurfaceView一样.确保您的子类实现了正确的构造函数,并将上下文和属性传递给父类:

public MyGLSurfaceView(Context context, AttributeSet attrs)
{
   super(context, attrs);
Run Code Online (Sandbox Code Playgroud)

然后你可以使用findViewById获取它:

MySurfaceView glSurfaceView = 
             (MySurfaceView)findViewById(R.id.surfaceviewclass);
Run Code Online (Sandbox Code Playgroud)

这应该够了吧.

  • 很奇怪它现在都在工作.结果你做了所有你说的,除了你需要用`glSurfaceView = new MyGLSurfaceView(this);`然后将xml文件设置为内​​容视图来获取glSurfaceView多么烦人.你能帮我更新答案吗? (3认同)