为什么 backgroundTint 需要 android: 在 API 21 之后而不是之前的前缀?
Why backgroundTint requires android: prefix after API 21 but not before?
声明一个样式,当我定义一个项目 android:backgroundTint
时,我收到一条警告,说它从 API 21 开始可用,而我指定的最小值 API 更低( API 17).另一方面,当我用 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>
除此之外,如果我对单个组件(例如按钮)使用 android:backgroundTint
,无论我的项目的最低 SDK 是多少,我都不会收到任何警告或错误。这有点令人费解。
为什么它在运行时工作:AppCompat 支持库向后移植了许多 API 级别的功能,其样式定义了 prefix-free backgroundTint
attribute.
lint 不抱怨的其他原因:style 没有前缀 android:
的属性未针对已知属性名称进行验证。您实际上可以将任何字符串放入 item
name
属性中。例如:
<item name="iJustInventedThis">foo</item>
在布局小部件中,您会收到 lint 抱怨缺少前缀或未知属性。如果您有一个 AppCompat 小部件,例如 AppCompatImageView
,那么您可以使用 backgroundTint
属性。
我知道这是一个老问题,但如果您遵循 指示我们将 android:backgroundTint
更改为 app:backgroundTint
,您将摆脱以下警告:
Attribute backgroundTint is only used in API level 21 and higher
声明一个样式,当我定义一个项目 android:backgroundTint
时,我收到一条警告,说它从 API 21 开始可用,而我指定的最小值 API 更低( API 17).另一方面,当我用 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>
除此之外,如果我对单个组件(例如按钮)使用 android:backgroundTint
,无论我的项目的最低 SDK 是多少,我都不会收到任何警告或错误。这有点令人费解。
为什么它在运行时工作:AppCompat 支持库向后移植了许多 API 级别的功能,其样式定义了 prefix-free backgroundTint
attribute.
lint 不抱怨的其他原因:style 没有前缀 android:
的属性未针对已知属性名称进行验证。您实际上可以将任何字符串放入 item
name
属性中。例如:
<item name="iJustInventedThis">foo</item>
在布局小部件中,您会收到 lint 抱怨缺少前缀或未知属性。如果您有一个 AppCompat 小部件,例如 AppCompatImageView
,那么您可以使用 backgroundTint
属性。
我知道这是一个老问题,但如果您遵循 android:backgroundTint
更改为 app:backgroundTint
,您将摆脱以下警告:
Attribute backgroundTint is only used in API level 21 and higher