SurfaceView从XML初始化

时间:2011-12-31 22:16:52

标签: android xml layout surfaceview

由于某些我无法理解的原因,当我从XML布局创建SurfaceView时,每次刷新时都会清除表面。但是,如果我只是在没有XML的代码中初始化它,则会保留绘制的表面。

如果我像这样初始化SurfaceView,则每次刷新时都会将其绘制为空白:

app = (App) findViewById(R.id.app);
app.setActivity(this);

如果我像这样初始化SurfaceView,每次刷新时都会保留曲面的图形:

app = new App(this);
setContentView(app);
app.setActivity(this);

这是XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:clickable="true">
        <com.threedtopo.someapp.android.app
            android:id="@+id/app"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/black"
            android:clickable="true" />
</LinearLayout>

任何见解都将不胜感激,谢谢!

编辑:这是在第一个示例中初始化根视图的方式:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
            <!-- <TextView -->
            <EditText
            android:id="@+id/hidden_text"
            android:inputType="textLongMessage"
            android:autoText="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:editable="true"
            android:visibility="gone"
            android:focusable="true"
            android:focusableInTouchMode="true"
            />
        </LinearLayout>
    <ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/splash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/splashscreen" />
        <com.threedtopo.someapp.android.app             android:id="@+id/app"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/white"
            android:clickable="true" />
    </ViewSwitcher> 
    <LinearLayout
        android:id="@+id/keyboard_layout"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard"
            android:visibility="gone"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="0" />
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

感谢harism,这已经解决了。显然,在XML文件中设置background属性会导致每次绘制时都清除SurfaceView。