13

我想利用 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 类中有很多东西,比如文件加载和触摸事件。只有我刚刚考虑过实施这种新布局

4

2 回答 2

22

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

public MyGLSurfaceView(Context context, AttributeSet attrs)
{
   super(context, attrs);

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

MySurfaceView glSurfaceView = 
             (MySurfaceView)findViewById(R.id.surfaceviewclass);

这应该够了吧。

于 2011-10-26T17:18:09.143 回答
1

如果一切正确,就像你在 xml-layout 中写的那样,Glsurfaceview-class 的完整路径:(和类名)

只有当类 GLSurfaceView 写在自己的文件中时,它才有效。在舒尔的这个文件中,需要正确编写构造函数。

我读到了 1 个用于 xml-refer 的构造函数,以及一个用于类之间通信的构造函数。如果正确编写,可以在 GLSurfaceView 内部找到用于 xml-refer 的构造函数和用于类之间通信的构造函数。GLSurfaceView,是你设置渲染器的地方,在xml构造函数中设置,必须是唯一的方法,它工作正常。(如答案1所示)

xml 构造函数:

public MyGLSurfaceView(上下文上下文,AttributeSet attrs){超级(上下文,attrs);setEGLContextClientVersion(2);
渲染器 = 新渲染器(上下文);设置渲染器(渲染器);

如果你们这些人中的一些人无法在 xml-layout 中使用 SurfaceView,他们从 Apress -Beginning 3D-Game-Development 购买了这本书。不要生气或伤害自己。在第 44-45 页将其写入一个文件中。像我的回答一样,在自己的文件中编写 GLSurfaceView。渲染器是自己的文件,其中:onSurfaceCreated、onSurfaceChanged、onDrawFrame..可以找到和MainActivity

于 2015-03-26T06:50:43.330 回答