Why backgroundTint requires android: prefix after API 21 but not before?

孤街醉人 提交于 2021-02-08 14:59:01

问题


Declaring a style, when I define an item android:backgroundTint, I get a warning that this is available as of API 21 onward, while my minimum API specified is lower (API 17). On the other hand, when I replace that with simply backgroundTint, the warning is gone. Does anyone know why is that?

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:backgroundTint">#0F0</item>
</style>

Aside, from that, if I use android:backgroundTint for a single component, for example a button, I get no warning or error, no matter what my project's minimum SDK is. This is somewhat puzzling.


回答1:


Why it works at runtime: AppCompat support library backports many of the later API level functionality and its styles define the prefix-free backgroundTint attribute.

Additional reason why lint does not complain: style attributes not prefixed with android: are not validated for known attribute names. You can actually put any string in item name attribute. For example:

<item name="iJustInventedThis">foo</item>

In layout widgets you get lint complaining about missing prefixes or unknown attributes. If you have an AppCompat widget such as AppCompatImageView, then you can use the backgroundTint attribute.




回答2:


I know it's an old question, but if you follow this solution that instructs us to change android:backgroundTint to app:backgroundTint, you get rid of the following warning:

Attribute backgroundTint is only used in API level 21 and higher



来源:https://stackoverflow.com/questions/48825405/why-backgroundtint-requires-android-prefix-after-api-21-but-not-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!