android - 给定 Activity 中的嵌套类,如何初始化自定义 surfaceView?

标签 android surfaceview android-view nested-class xml-layout

我有一个看起来像这样的自定义 SurfaceView:

public class GFXSurface extends Activity implements OnTouchListener 
{
    .
    .
    GameSurface gameSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game); 
        gameSurfaceView = (GameSurface)findViewById(R.id.svgame);
        fullscreen();
        initialize();
    }
    .
    .
    .
    public class GameSurface extends SurfaceView implements Runnable 
    {
        .
        .
        public GameSurface(Context context) 
        {
            super(context);
            ourHolder = getHolder();
        }
        .
        .
        .
    }
}

我的问题是如何在我的 xml 布局中引用它?

我正在使用 xml 布局,因为我需要添加一些 表面 View 之上的东西

我尝试了几种方法但都失败了...

这行不通:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <com.bla.bla.GFXSurface.GameSurface
    android:id="@+id/svgame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
 .
 .
 .
</FrameLayout>

非常感谢您的帮助!!!!

最佳答案

您的GameSurface 类需要是静态的。您不能在布局中使用非静态嵌套类,因为您需要外部类实例来创建此类的对象。

关于android - 给定 Activity 中的嵌套类,如何初始化自定义 surfaceView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580096/

相关文章:

android - 向旧 Activity 发送数据

android - 在横向模式下视频 View 不是全屏

android - 缩放 SurfaceView 时视频不缩放

android - 如果 SurfaceView 尺寸不是支持的尺寸之一,camera2 将崩溃

android - 在 Android 中隐藏 RadioButton

android - 网站未出现在 WebView 中

c# - 双对象内存泄漏?

android - 如何在android状态栏上的图标上显示数字

java - 如何退出SurfaceView?

android - View.getId() 不工作