“可能的透支:根元素绘制背景”

时间:2012-08-16 19:51:26

标签: android android-manifest android-theme

在我的项目上运行Android Lint时,我遇到了这个警告

  

可能的透支:根元素绘制背景@ drawable / main with   一个也描绘背景的主题

推断主题为@android:style/Theme.NoTitleBar.Fullscreen

有人可以向我解释为什么会这样,以及如何删除它?

我的xml:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/main" //***LINT warning***
        android:orientation="vertical"
        android:weightSum="3" >

清单中定义主题的部分

 <application
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

5 个答案:

答案 0 :(得分:19)

要优化应用效果(避免透支),您可以执行以下操作:

  • res/values/styles.xml

    中声明主题

    <style name="MyTheme" parent="android:Theme">
        <item name="android:background">@drawable/main</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    

  • 更改清单:

    <application
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >
    
  • 删除“我的xml”
  • 中的背景声明

答案 1 :(得分:12)

<强>更新

查看评论或查看this link。正如Marcin所提到的,我的解决方案并不是一个好方法,因为它可能会导致人工制品。我一直用它来避免长时间的过度抽取而没有任何问题,但根据Chet Haase对这种技术的评论,一般来说可能不是规则的大拇指。

原始回答

我发现的最佳方法是将默认背景设置为null,并在每个布局中应用所需的背景。

原因是当您在主题中设置默认背景,或者甚至是如上所述的具有不同背景的不同主题时,这意味着整个布局将填充该背景。在大多数情况下,您不需要填充100%的背景,因为在该背景上有工具栏,页眉,页脚和其他元素会导致过度绘制。

在主题上应用空背景:

<style
    name="ThemeName" parent="ParentTheme">
    <item name="android:windowBackground">@null</item>
</style>

要检查overdraw,只需激活模拟器/设备上dev选项中的Show Overdraw选项。不要相信100%lint警告,因为跟踪器中有一些我不确定是完全修复的错误。

更多信息: What is overdraw and why is an issue?

答案 2 :(得分:2)

你得到这个lint警告的原因是你的活动和你的线性布局都试图绘制背景。因此,可见区域被绘制两次。

如何调试?运行sdk / tools / hierarchyviewer并检查视图层次结构以查看哪个视图具有未显示的背景。 (你需要有一个运行dev build rom的android设备)

什么在幕后运行?请注意,几乎所有Android主题都指定了背景,这意味着如果你想创建一个覆盖整个屏幕和背景的“LinearLayout”,你有更好的是设置活动的windowBackground =“@ null”或删除线性布局中的背景设置。

答案 3 :(得分:0)

如果在replace中有彼此FragmentManager的片段,则可以删除背景的其他图形。

所以,如果有

val fragment = YourFragment.newInstance()
parentFragmentManager.beginTransaction().run {
    replace(R.id.container, fragment, YourFragment.TAG)
    addToBackStack(YourFragment.TAG)
}.commitAllowingStateLoss()

然后该片段将替换一个当前片段,并具有活动背景。在这种情况下,您可以省略片段的android:background="@color/..."

但是,如果您删除android:background并使用add删除当前片段上方的片段,则它将具有透明背景,因此视图将彼此重叠。您会看到2个片段,一个在另一个之上。

要检查此行为,您可以按照上面的建议在<item name="android:windowBackground">@color/...</item>的{​​{1}}中临时添加AppTheme

答案 4 :(得分:0)

更暴力的方法。在您的自定义 theme.xml

<style name="MyTheme" parent="Theme.MaterialComponents.Light.Bridge">
     ...
    <item name="android:windowBackground">@color/white</item>
     ...
</style>