Android ?可提取资源中的属性导致资源$NotFoundException
在图层列表中,我在一些项目中使用了solid,并使用Android ?可提取资源中的属性导致资源$NotFoundException,android,drawable,attr,Android,Drawable,Attr,在图层列表中,我在一些项目中使用了solid,并使用?attr/text\u color设置颜色 并将此可绘制设置为按钮的背景android:background=“@drawable/myLayerListDrawable” 在我在较低的api18上运行这个项目之前,我一直在使用它,没有任何问题 原因:android.content.res.Resources$NotFoundException:文件 res/drawable/myLayerListDrawable.xml来自可绘制资源ID
?attr/text\u color
设置颜色
并将此可绘制设置为按钮的背景<代码>android:background=“@drawable/myLayerListDrawable”
在我在较低的api18
上运行这个项目之前,我一直在使用它,没有任何问题
原因:android.content.res.Resources$NotFoundException:文件
res/drawable/myLayerListDrawable.xml来自可绘制资源ID#0x7f080063
原因:java.lang.UnsupportedOperationException:无法转换为
颜色:类型=0x2
为什么会发生这种情况?如何解决
res\drawable\myLayerListDrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/background_button_pressed" />
<stroke
android:width="0.7dp"
android:color="?attr/text_color" />
<corners android:radius="10dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<stroke
android:width="0.7dp"
android:color="?attr/text_color" />
<corners android:radius="10dp" />
</shape>
</item>
</selector>
</item>
-----------------------------------------------------------------------------------------
最终答案
我们不能在api 21之前的xml可绘制资源中使用?attr。可牵引
aapt在编译时创建的资源。用于
运行时的动态连接
解决方案是为每个主题创建不同的绘图。您无法访问像
“?attr/myColor
欲了解更多信息,请阅读
而不是
<stroke android:color="?attr/myColor" />
并确保您已经在res/values/colors.XML中添加了
myColor
,您必须在样式或属性文件中添加引用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="myColor" format="reference" />
</resources>
并将此添加到您的主题中:
<item name="myColor">#c3c3c3</item>
#c3c3
应该是
@NileshRathod我正在为不同的主题使用不同的颜色,但是你不能像“?attr/myColor”
那样访问颜色。你如何在可绘图资源中使用基于主题的颜色?@Mehran检查这个答案@Mehran测试用例试试这个方法android:color=“?myColor=”“
你说得对。在preAPI 21
中,无法引用绘图中的颜色。解决方案是为每个主题创建不同的绘图。
<item name="myColor">#c3c3c3</item>