【问题标题】:Error inflating ImageView / ImageButton with ColorStateList tint value使用 ColorStateList 色调值膨胀 ImageView / ImageButton 时出错
【发布时间】: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


【解决方案1】:

在我的例子中,我将android:tint 替换为app:tint 并添加到根元素xmlns:app="http://schemas.android.com/apk/res-auto"。它修复了 API 级别

颜色状态选择器 /res/color/color_selector.xml 看起来像:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#151515"/>
    <item android:state_focused="true" android:color="#151515"/>
    <item android:color="#424242"/>
</selector>

【讨论】:

    【解决方案2】:

    看起来 AppCompat 可绘制着色仅适用于

    当您设置这些属性时,AppCompat 会自动将它们的值传播到 API 21+ 上的框架属性。这会自动为状态栏和概览(最近)任务条目着色。

    在旧平台上,AppCompat 会尽可能模拟颜色主题。目前这仅限于为操作栏和一些小部件着色。

    来源:https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html

    这里的答案还有更多细节:https://stackoverflow.com/a/29155611/608312

    【讨论】:

    • 着色本身可以工作,我可以在源代码中看到它。我是在专门谈论 ColourStateList 着色,它似乎再次出现在源代码中,但我无法在任何地方找到关于这是否得到官方支持的定义
    【解决方案3】:

    item name="android:tint" 错误。

    将 'android:tint' 修复为 'tint'

    <style name="IconButton.Primary">
        <item name="tint">@color/link_button_color</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-28
      • 2013-05-17
      • 1970-01-01
      • 2018-03-04
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多