【发布时间】:2018-05-23 14:23:31
【问题描述】:
使用 ImageView / ImageButton (AppCompatImageView / AppCompatImageButton) 与 android:tint 的样式属性结合使用 ColorStateList 资源在 >= API 21 上可以正常工作,但会抛出API InflateException。
首先,我什至不知道AppCompatImageView / (Button) 着色是否支持ColourStateList xml 资源作为android:tint 值,我似乎无法找到明确的答案。我可以在 S/O 上找到的建议建议实现 TintableImageView 等,但这些答案已经过时了,而且从 appcompat 实现的来源看来,这应该是一个功能。
澄清这绝对是问题所在。删除 android:tint 属性或将其设置为单一颜色资源有效。
另外澄清一下,我知道这是可以通过编程实现的。我试图让它在 xml 中向后兼容。
小例子
activity_foo.xml
<android.support.v7.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_caret_up"
style="@style/IconButton.Primary"
/>
styles.xml
<style name="IconButton.Primary">
<item name="android:tint">@color/link_button_color</item>
</style>
link_button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/btnLinkPressedTextColor"
android:state_selected="true" />
<item android:color="@color/btnLinkPressedTextColor"
android:state_pressed="true" />
<item android:color="@color/btnLinkTextColor" />
</selector>
【问题讨论】:
-
你可以尝试直接替换十六进制代码而不是从@color获取它
-
使用ImageViews时会出现这种情况吗? stackoverflow.com/questions/29155463/…
-
它发生在使用 ImageView、ImageButton(在编译时被它们的 appcompat 变体替换)或显式指定它们的 appcompat 变体时,如我提供的示例所示。
标签: android imageview android-appcompat android-4.4-kitkat tint