2011-11-17 38 views
12

我定义如下绘制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一切就好了错误?

回答

25

A ColorStateList无法作为<solid>的属性在XML定义中传递,或者确实属于<shape>的任何属性。该属性作为Color资源从XML中扩充出来,然后传递给Drawable的setColor()方法,该方法只需要一个ARGB值。

只有一种类型的Drawable实例被设计为包含并呈现基于状态的多个项目,即StateListDrawable,这是您在给一个<selector>充气时所得到的结果。所有其他可绘制实例都只是作为此集合的成员或绘制的独立成员。

还要注意充气<shape>项目实际上是一个GradientDrawable而不是ShapeDrawable。如果您查看inflate()方法GradientDrawablein the source,您可以获得关于每个属性如何使用的所有详细信息。

HTH!

+7

从Android棒棒糖开始,这不再是真的,并且'ColorStateList'被正确解析。 – keyboardr

+1

可以确认它是否适用于棒棒糖 - 真的很困惑为什么我的<21测试设备无法正常工作! – Tom

+7

预棒棒糖设备的显而易见的(和可怕的)替代方案是创建尽可能多的'',因为有状态,每个只有'颜色'属性不同,并替换原来的''可绘制的引用每个''的'StateListDrawable'。事物的暴行可以通过使用''元素中唯一的资源引用(没有硬编码值)可稍有缓解,而人们也可以有原始''保持在'绘制-v21'目录(以完全可能切换到以后)。 _blargh_ – desseim

-4

你正在做它拧....只是替换此

android:color="@color/color_stateful" 

android:background="@color/color_stateful" 

更新:

在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:background="@color/color_stateful" /> <!--this is the chanage i made... here--> 
     </shape> 
    </item> 

    <item android:drawable="@drawable/selector_png_drawable" /> 
</layer-list> 
+1

在drawable的形状中没有'android:background'这样的属性 –

相关问题