【问题标题】:SurfaceView Initialized from XML从 XML 初始化的 SurfaceView
【发布时间】:2012-01-31 05:29:03
【问题描述】:

由于某种我不明白的原因,当我从 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>

【问题讨论】:

  • 这可能会或可能不会影响差异,但在后一种情况下,您将 App 分配为根元素,并且您不指定背景颜色。
  • 感谢 harism,我为第一个示例添加了如何初始化根显示。请注意,我尝试从所有 XML 布局中删除背景颜色,但这并没有改变行为。
  • 好的 - 所以你一定是正确的。设置背景颜色似乎是罪魁祸首。我猜在我清理之后,对 XML 文件的更改并没有做任何事情。
  • harism 如果您发表您的评论作为答案,我会支持它。谢谢!

标签: android xml layout surfaceview


【解决方案1】:

多亏了harism,这个问题已经解决了。显然在 XML 文件中设置背景属性会导致 SurfaceView 每次绘制都被清除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多