可能透支:根元素使用同样绘制背景的主题绘制背景

Possible overdraw: Root element paints background with a theme that also paints a background

我已经实现了可点击的 Recyclerview 项并为点击效果设置了 android:background="?selectableItemBackground" 但是在检查代码时我发现了这个 lint 问题。

棉绒警告: 可能透支:根元素绘制背景 ?selectableItemBackground 的主题也绘制背景

有解决此警告的想法吗?

我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackground"
    android:clickable="true"
    android:orientation="vertical"
    android:padding="@dimen/row_padding">

    //...
</LinearLayout >

你应该把波纹放在前景属性上

默认情况下,主题具有指定的 android:windowBackground 属性,顾名思义,它指定 window 的背景,您的 activity 将在此处启动。

此 lint 警告仅告诉您以下内容:

Hey! I see your theme has a windowBackground applied, and your root layout draws some other drawable on top of window background, making window's background pointless - thus overdrawing pixels needlessly.

取消 windowBackground 将使 lint 不会抱怨:

<style name="AppTheme" parent="...">
    ...
    <item name="android:windowBackground">@null</item>
</style>

我只想删除 lint 检查。对于大多数应用程序来说,透支 x1 是完全可以接受的。

Romain Guy 那里有一篇关于透支和性能的非常好的post:http://www.curious-creature.com/2012/12/01/android-performance-case-study/

在您的主题中设置 <item name="android:windowBackground">@null</item> 是错误的,因为它会删除您的 activity 启动动画(有关启动动画的更多详细信息,请参阅 post:https://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/) .你不应该那样做。

在 Activity.onCreate() 中设置 getWindow().setBackgroundDrawable(null) 是可行的,但您必须确保应用中的每个像素都至少绘制一次,因为您没有背景可以绘制了。对于非常有限的收益,它具有潜在的危险。