【问题标题】:Overdraw issue in fragment layout with both background color and theme existing存在背景颜色和主题的片段布局中的过度绘制问题
【发布时间】:2018-07-21 20:49:26
【问题描述】:

您好,我是使用 AS 进行 Android 编程的新手。这应该是一个常见/简单的问题,但到目前为止我还没有找到解决方案。

我在activity_main.xml 中为平板电脑创建left_right_fragments 标签,我想在right_fragment 中设置背景颜色(暂时设置为#ffffff 并保持left_fragment 为默认值)。然后Android Lint 警告我在right_fragment 上得到了Overdraw: Painting regions more than once

我尝试创建一个新主题:

<style name="NoBgTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <item name="android:windowBackground">@null</item>
</style>

并将android:theme="@style/NoBgTheme"添加到right_fragment.xml,但没有成功。

Screenshot of this app w/ Debug GPU Overdraw enabled.

如果我在AndroidManifest.xml 中将android:theme="@style/AppTheme" 更改为"@style/NoBgTheme",它可以工作,但所有其他布局都失去了背景。

知道如何修复这个 OVERDRAW 吗?提前谢谢。


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/left_fragment"
        android:name="jerryc05.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="jerryc05.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

left_fragment.xml 非常简单,我们可以忽略它。

right_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/NoBgTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <TextView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is right fragment"
        android:textSize="20sp"
        tools:ignore="HardcodedText" />

</LinearLayout>

AndroidManifest.xml 保持不变。

MainActivity.java 保持不变。

【问题讨论】:

    标签: android layout fragment gpu-overdraw


    【解决方案1】:

    会发生一点透支。如果要修复左片段和右片段显示上的过度绘制,则需要将 nobgtheme 设置为主要活动。如果这只发生在平板电脑布局期间,您可以检查这是否是正在使用的平板电脑布局,并使用此处描述的方法在运行时设置无背景主题:How to change current Theme at runtime in Android

    【讨论】:

    • 感谢您的回答。但是在运行时设置主题并没有解决问题。它的工作原理与将android:theme="@style/AppTheme" 更改为AndroidManifest.xml 中的"@style/NoBgTheme" 一样,这导致left_fragment 失去了显示一些有线模式的背景,尽管right_fragment 以这种方式正常工作。你看,MainActivity同时显示了两个fragment,只有右边的有overdraw问题,而不是整个activity。
    • 没关系,兄弟,我在下面发布了一个替代解决方案。顺便说一句,你有更好的解决方案,不需要改变活动的主题(只改变right_fragment)?
    【解决方案2】:

    好的,我有一个替代解决方案。

    我在&lt;activity android:name=".MainActivity" 之后在&gt; 之前添加了android:theme="@style/NoBgTheme"

    然后我手动将android:background="#f0f0f0"android:background="#ffffff"分别指定为left_right_fragment.xml

    效果很好!!!

    但 Android Lint 警告仍然存在......(这是一个错误还是我的错?)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多