【发布时间】: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/.. 调用。我没有使用任何特定于版本的资源文件夹。
我在这里做错了什么?
【问题讨论】: