如圖自定義的Switch樣式:
需要注意的是:switch的大小是跟thumb的大小有關(guān)
下面以樣式二為例:
switch_track_selector_new.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_track_bg_selected" android:state_checked="true"/>
<item android:drawable="@drawable/switch_track_bg_normal" android:state_checked="false"/>
</selector>
switch_track_bg_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/switch_track_selected"/>
<size android:width="@dimen/switch_track_width"
android:height="@dimen/switch_track_height"/>
<corners android:radius="20dp"/>
</shape>
switch_track_bg_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/switch_track_normal"/>
<size android:width="@dimen/switch_track_width"
android:height="@dimen/switch_track_height"/>
<corners android:radius="20dp"/>
</shape>
switch_thumb_selector_new.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_thumb_bg_selected" android:state_checked="true"/>
<item android:drawable="@drawable/switch_thumb_bg_normal" android:state_checked="false"/>
</selector>
switch_thumb_bg_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="@dimen/switch_thumb_stroke_width" android:color="@color/switch_track_selected"/>
<solid android:color="@color/switch_thumb_normal"/>
<size android:width="@dimen/switch_track_height"
android:height="@dimen/switch_track_height"/>
</shape>
switch_thumb_bg_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="@dimen/switch_thumb_stroke_width" android:color="@color/switch_track_normal"/>
<solid android:color="@color/switch_thumb_normal"/>
<size android:width="@dimen/switch_track_height"
android:height="@dimen/switch_track_height"/>
</shape>
switch引用:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_thumb_selector_new"
android:track="@drawable/switch_track_selector_new"/>
詳細(xì)參考: Android Switch自定義樣式