【发布时间】:2019-04-29 04:44:27
【问题描述】:
在图层列表中,我在某些项目中使用了纯色并使用?attr/text_color 设置颜色。
<stroke android:color="?attr/text_color" />
并将此可绘制对象设置为按钮的背景。 android:background="@drawable/myLayerListDrawable"
在我在较低的 api 18 上运行此项目之前,我一直在使用它而没有任何问题。
原因: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>
------------------------------------------ -------------------------------------------------- --------
最终答案
我们不能在 xml 可绘制资源 pre api 21 中使用 ?attr。可绘制 aapt 在编译时创建的资源。 Attr 资源用于 运行时动态连接。
解决方案是为每个主题创建不同的绘图。
【问题讨论】:
-
应该是
<stroke android:color="@color/myColor" /> -
@NileshRathod 我为不同的主题使用不同的颜色
-
好的,但是你不能像
"?attr/myColor"那样访问颜色