【问题标题】:Android View rendering errors on screen reorientation?Android View 在屏幕重定向时出现渲染错误?
【发布时间】:2014-06-18 17:44:42
【问题描述】:

我在一个非常基本的应用程序上遇到了这种奇怪的行为,在屏幕重定向后视图呈现错误。

除了视图显示之外,当屏幕重新定向时(使用活动破坏/创建,因为它没有拦截这些事件),应用程序中的代码会按预期工作。

当屏幕重新定向时,视图可以正常工作,只是屏幕重新定向时的任何样子都会叠加一个幻影图像。它看起来像“烧录”的 CRT 重影。

以任何方向启动应用程序都可以正常运行,仅当屏幕在运行时重新定向时才会出现此问题。

视图在一个 Fragment 中使用,它是其 Activity 中唯一的一个。 ListView、DatePicker 和 TextView 的情况相同。

该问题出现在多个设备中: - 运行 4.4.3 stock android 的 nexus 4 - 三星 Galaxy 运行 cyanogenmod 10.2.1 (android 4.3.1) - API 级别 18 (android 4.3) 的“galaxy nexus”AVD

知道这里发生了什么吗?还有哪些信息有助于解决此问题?

以下截图:

下面 ListView 示例的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_expenses"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Row for Buttons -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="3dp" >

        <Button
            android:id="@+id/list_expenses_button_add"
            android:layout_width="0dip"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="@string/label_button_listexpenses_add" />

     </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/label_listexpenses_noexpenses"
        android:gravity="center" />
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    问题在于,由于 Activity 类中的以下代码,片段被重复添加到布局中,导致视图重叠(毕竟不是呈现错误):

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_expenses);
    
        if (getSupportFragmentManager().findFragmentById(R.id.list_expenses) == null) {
            fragment = ListExpensesFragment.newInstance();
            getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
        } else {
            fragment = getSupportFragmentManager().findFragmentById(R.id.list_expenses);
        }
    }
    

    解决方案:将对getSupportFragmentManager().beginTransaction().add()的调用替换为getSupportFragmentManager().beginTransaction().replace()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-13
      • 2012-12-11
      • 2018-05-15
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多