我定义如下绘制my_background_drawable.xml
:颜色列表中的形状绘制对象不承认
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
<item android:drawable="@drawable/selector_png_drawable" />
</layer-list>
而且我还定义了以下颜色状态列表资源color_stateful.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#FF00ff00"/>
<item android:color="#FFff0000"/>
</selector>
当我设置给my_background_drawable
作为背景对于某些视图,我无法观察color_stateful.xml
中为我的形状定义的颜色的任何更改,而视图状态实际上已更改(selector_png_drawable.xml
是一个指示器)。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This doesn't work
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="@color/color_stateful" />
</shape>
</item>
-->
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FF00ff00" />
</shape>
</item>
<item>
<shape android:gravity="center"
android:shape="rectangle">
<solid android:color="#FFff0000" />
</shape>
</item>
</selector>
</item>
<item android:drawable="@drawable/selector_png_drawable"" />
</layer-list>
因此,它是真实的色彩状态信息只是当ColorStateList
资源是ShapeDrawable
内使用或丢失我在做它:
然而,当我以下列方式修改我的my_background_drawable.xml
一切就好了错误?
从Android棒棒糖开始,这不再是真的,并且'ColorStateList'被正确解析。 – keyboardr
可以确认它是否适用于棒棒糖 - 真的很困惑为什么我的<21测试设备无法正常工作! – Tom
预棒棒糖设备的显而易见的(和可怕的)替代方案是创建尽可能多的'',因为有状态,每个只有'颜色'属性不同,并替换原来的''可绘制的引用每个''的'StateListDrawable'。事物的暴行可以通过使用''元素中唯一的资源引用(没有硬编码值)可稍有缓解,而人们也可以有原始''保持在'绘制-v21'目录(以完全可能切换到以后)。 _blargh_ –
desseim