gpt4 book ai didi

Android drawable xml 无法转换为 API 10 的颜色异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:35 27 4
gpt4 key购买 nike

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

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/.. 调用设置在其他资源文件。我没有使用任何版本特定的资源文件夹。

我在这里做错了什么?

最佳答案

我在 SO 上找到了一个答案,提到 ?attr/.. 在 xml 可绘制对象中不受支持。看这里:https://stackoverflow.com/a/13471695/169748

显然这至少对于 API <= 10 是正确的。

关于Android drawable xml 无法转换为 API 10 的颜色异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com