Android Selector Drawable不适用于属性 [英] Android Selector Drawable doesnt work with attributes
问题描述
我正在使用attr为我的项目创建一个可绘制的选择器,这样,一旦我更改主题颜色,就不必在可绘制文件中进行任何更改.我正在使用以下库:
I am using attr to create a selector drawable for my project so that once i change theme colors, i dont have to make any change in the drawable file. I am using following libs:
compile 'com.android.support:appcompat-v7:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:design:22.2.0'
这是drawable的源代码:
Here is the source code for drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="?attr/colorPrimary"/>
</selector>
在同一代码中,如果我将颜色替换为colors.xml文件中定义的颜色,则相同的可绘制对象将起作用.
in this same code, if i replace attributes with colors defined in colors.xml file, the same drawable works.
可绘制颜色的样本:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_primary" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@color/color_primary_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
<item android:drawable="@color/color_primary"/>
</selector>
提前谢谢!
推荐答案
最后,找到了问题所在. android [pre-lollipop]操作系统中存在一个错误,该错误不允许您在drawable中使用attr.这是错误的链接:
Finally, found the problem. There is a bug in android [pre-lollipop] OS which doesnt allow you to use attr in drawable. Here is the link to bug:
Android开发人员团队已发布了修复程序,但可在android L及更高版本上运行.有关此问题的解决方法,请参阅以下解决方案:
Android dev team has released a fix but it works on android L and above.For workaround to this problem, refer to following solution:
这篇关于Android Selector Drawable不适用于属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!