【问题标题】:Android drawable xml Can't convert to color Exception for API 10Android可绘制xml无法转换为API 10的颜色异常
【发布时间】:2014-11-16 12:21:48
【问题描述】:

在我的应用程序中,我定义了在自定义主题中设置的颜色属性:

res/values/attrs.xml

<resources>
    <attr name="bbColorPrimary" format="color|reference" />
</resources>

res/values/colors.xml

<resources>
    <color name="white">#ffffff</color>
</resources>

res/values/style.xml

<style name="MyStyle" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="bbColorPrimary">@color/white</item>
</style>

res/drawable/background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape>
            <solid android:color="?attr/bbColorPrimary" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

res/drawable/background.xml 设置为某个按钮的背景。应用程序在膨胀 res/drawable/background.xml 文件时崩溃,以下情况除外:

...
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
            at android.content.res.TypedArray.getColor(TypedArray.java:326)
            at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:748)
            at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
            at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:172)
....

它适用于高于 10 的 API 级别。如果我删除 ?attr/bbColorPrimary 它可以正常工作,尽管在其他资源文件中设置了许多其他 ?attr/.. 调用。我没有使用任何特定于版本的资源文件夹。

我在这里做错了什么?

【问题讨论】:

    标签: android android-resources


    【解决方案1】:

    我在 SO 上找到了答案,提到 ?attr/.. 在 xml drawables 中不受支持。见这里:https://stackoverflow.com/a/13471695/169748

    显然,至少对于 API

    【讨论】:

    • 即使 API = 16 我也崩溃了,但 API => 21 没问题。
    • @Omcira 谢谢你提到这一点。也可以用 API = 15 确认。
    • 即使使用 API 17,我认为它在
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2020-10-26
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多