切换按钮字体颜色和drawableft选择器

时间:2016-08-08 09:38:22

标签: android xamarin

Xamarin,android。 我做了一个改变背景颜色的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="false">
    <shape>
      <solid
          android:color="#aaaaac"/>

      <corners
          android:radius="6dp" />
      <padding
          android:left="5dp"
          android:top="5dp"
          android:right="5dp"
          android:bottom="5dp" />
    </shape>
    <drawable
      drawableLeft="@drawable/male"/>
  </item>
  <item android:state_checked="true">
    <shape>

      <solid
          android:color="#357fca" />

      <corners
          android:radius="6dp" />
      <padding
          android:left="5dp"
          android:top="5dp"
          android:right="5dp"
          android:bottom="5dp" />
    </shape>
    <drawable
          drawableLeft="@drawable/male_pushed"/>
  </item>
</selector>

除此之外,我还想更改drawableft和文本颜色,如下所示。 如果 - android:state_checked="false" 然后
android:textColor="#000000"android:drawableLeft="@drawable/male"

if - android:state_checked="true" 然后是android:textColor="#ffffff"

android:drawableLeft="@drawable/male_pushed"

怎么做?

1 个答案:

答案 0 :(得分:1)

您无法使用单一选择器设置多个属性,如drawableLefttextColor等。您应该为每个属性创建选择器。

创建

  1. <强> drawable_left_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@drawable/male"/>
        <item android:state_checked="true" android:drawable="@drawable/male_pushed"/>
    </selector>
    
  2. <强> text_color_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:color="#000000"/>
        <item android:state_checked="true" android:color="#ffffff"/>
    </selector>
    
  3. <强> toggle_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false">
            <shape>
                <solid android:color="#aaaaac"/>
                <corners android:radius="6dp" />
                <padding
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp" />
           </shape>
        </item>
        <item android:state_checked="true">
            <shape>
    
                <solid android:color="#357fca" />
                <corners android:radius="6dp" />
                <padding
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp" />
            </shape>
        </item>
    </selector>
    
  4. 设置ToggleButton

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toggle_selector"
        android:drawableLeft="@drawable/drawable_left_selector"
        android:textColor="@drawable/text_color_selector" />