为什么 backgroundTint 需要 android: 前缀在 API 21 之后而不是之前?

cod*_*bie 6 android xml-namespaces android-styles

声明一个样式,当我定义一个 item 时android:backgroundTint,我收到一个警告,它从 API 21 开始可用,而我指定的最小 API 较低(API 17)。另一方面,当我用 simple 替换它时backgroundTint,警告消失了。有谁知道这是为什么?

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

除此之外,如果我android:backgroundTint用于单个组件,例如按钮,则无论我的项目的最低 SDK 是什么,我都不会收到警告或错误。这有点令人费解。

Jor*_*mVF 6

我知道这是一个老问题,但是如果您遵循指示我们更改为 的解决方案,您将摆脱以下警告:android:backgroundTintapp:backgroundTint

属性backgroundTint仅在API级别21及更高版本中使用


laa*_*lto 4

为什么它在运行时起作用:AppCompat 支持库向后移植了许多后续 API 级别的功能,并且其样式定义了无前缀backgroundTint属性

lint 不抱怨的另一个原因是:没有前缀的样式android:属性不会针对已知的属性名称进行验证。实际上,您可以将任何字符串放入item name属性中。例如:

<item name="iJustInventedThis">foo</item>
Run Code Online (Sandbox Code Playgroud)

在布局小部件中,您会收到 lint 抱怨缺少前缀或未知属性的信息。如果您有 AppCompat 小部件(例如 )AppCompatImageView,则可以使用该backgroundTint属性。