java - 将 GLSurfaceView 类与 android xml 布局一起使用

标签 java android android-layout opengl-es glsurfaceview

我想使用 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/

相关文章:

android - 图像切换器无法正常工作

java - Android I/O 2014 演示应用问题

android - 我在哪里可以找到 Android Studio 2.2 Preview 1 中的新布局检查器

java - 正则表达式不应该发生灾难性的回溯

java.lang.NoClassDefFoundError : org/hibernate/type/StandardBasicTypes - What could be the reason? 错误

java - Java 对象可以成为 Nashorn 中 JavaScript 对象的原型(prototype)吗?

android - 如何在 Jetpack Compose 中让底页覆盖整个屏幕

java - 在 4.4 设备上运行的应用程序的主题问题

android - 如何使自定义 windowBackground 主题样式适用于所有 Android 设备?

javax.websocket 客户端简单示例