1. 程式人生 > >Android Switch修改樣式

Android Switch修改樣式

1.定義Switch滑塊顏色、關閉狀態顏色和開啟狀態顏色

<!--Switch滑塊顏色-->
<color name="switch_thumb">#FFFFFF</color>
<!--Switch關閉狀態顏色-->
<color name="switch_off">#AAAAAA</color>
<!--Switch開啟狀態顏色-->
<color name="switch_on">#99CC00</color>
2.定義Switch滑塊選擇器和狀態選擇器
<!--Switch_thumb_selector-->
<item android:state_checked="false"> <shape android:shape="oval"> <solid android:color="@color/switch_thumb" /> <size android:width="30dp" android:height="30dp" /> <stroke android:width="2dp" android:color="@color/switch_off" /> </shape> </item>
<item android:state_checked="true"> <shape android:shape="oval"> <solid android:color="@color/switch_thumb" /> <size android:width="30dp" android:height="30dp" /> <stroke android:width="2dp" android:color="@color/switch_on" /> </shape> </item>
<!--switch_track_selector-->
<item android:state_checked="false">
    <shape android:shape="rectangle">
        <solid android:color="@color/switch_off" />
        <corners android:radius="30dp" />
    </shape>
</item>
<item android:state_checked="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/switch_on" />
        <corners android:radius="30dp" />
    </shape>
</item>
3.在xml中為Switch設定選擇器
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_track_selector" />
4.修改Switch大小

修改Switch滑塊的size

<size android:width="30dp" android:height="30dp" />
修改Switch屬性的switchMinWidth
android:switchMinWidth="100dp"