开发者

Android - problems with ColorStateList xml referenced from shape xml

I'm running into a problem where the color selector in one of my drawable xml files doesn't seem to be honored. I have a layout including:

<LinearLayout 
    android:layout_height="wrap_content"
    android:id="@+id/sortLayout"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:background="@color/listSortBarBackground" android:orientation="vertical">
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="true"
        android:background="@drawable/filter_button_left"/>
    <ToggleButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:checked="false"
        android:background="@drawable/filter_button_left"/>
</LinearLayout>

drawable\filter_button_left.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
开发者_如何学C  <solid android:color="@color/filter_button_color" />
  <corners
    android:bottomLeftRadius="0dp"
    android:topLeftRadius="5dp"
    android:topRightRadius="0dp"
    android:bottomRightRadius="5dp"/>
</shape>

and color\filter_button_color.xml is:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:color="@color/myred"
    android:state_checked="true"/>
  <item
    android:color="@color/myblue"
    android:state_checked="false"/>
</selector>

(myred and myblue are defined in colors.xml)

The buttons are rendered and I can tell they are getting the proper checked state since the first one displays with text "ON" and the second one "OFF", both buttons get the shape as their background, but in both cases the shape's color is myred. I played around with flipping the items in the filter_button_color.xml selector, and it seems that regardless of the actual state or the states in the selector items, the top color is always used.

Can anyone see why this shouldn't work?

Thanks! Scott


I think your problem is that you have the xml drawables the wrong way around.
You need to refer to your selector FIRST from the ToggleButton layout, and inside the selector layout, have the two drawables.
Eg ToggleButton -> Selector (With two states) -> Shapes.
This should work (and make one less xml file).

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/myred"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <solid android:color="@color/myblue"/>
            <corners 
                android:bottomLeftRadius="0dp" 
                android:topLeftRadius="5dp" 
                android:topRightRadius="0dp" 
                android:bottomRightRadius="5dp"/>
        </shape>
    </item>
</selector>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜