2016-08-08 42 views
3

Xamarin,android。 我做了一個選擇,其改變背景顏色:Toogle button font color and drawableft selector

<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"

如果 - android:state_checked="true" 然後android:textColor="#ffffff"

android:drawableLeft="@drawable/male_pushed" 

如何做到這一點? 謝謝

回答

1

您不能使用單選擇器設置多個屬性,如drawableLeft,textColor等。您應該爲每個屬性創建選擇器。

創建

  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" /> 
    
+0

** ** SJY,謝謝 – Kobra

相關問題