Android Swicth按钮样式自定义
<Switch
android:id="@+id/switch_btn"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginRight="10dip"
android:checked="true"
android:splitTrack="false"
android:switchMinWidth="50dp"
android:thumb="@drawable/ko_switch_thumb_white"
android:track="@drawable/ko_switch_track_primary"
android:typeface="serif" />
ko_switch_thumb_white和ko_switch_track_primary文件定义Switch的样式。
ko_switch_thumb_white.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ko_switch_thumb_on" android:state_checked="true" />
<item android:drawable="@drawable/ko_switch_thumb_off" android:state_checked="false" />
</selector>
ko_switch_track_primary.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ko_switch_track_on" android:state_checked="true" />
<item android:drawable="@drawable/ko_switch_track_off" android:state_checked="false" />
</selector>
ko_switch_thumb_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/ko_white" />
<size
android:width="24dp"
android:height="24dp" />
<stroke
android:width="5dp"
android:color="#00000000" />
</shape>
ko_switch_thumb_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/ko_white" />
<size
android:width="24dp"
android:height="24dp" />
<stroke
android:width="5dp"
android:color="#00000000" />
</shape>
ko_switch_track_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/ko_color_primary" />
<stroke
android:width="0dp"
android:color="#00000000" />
<corners android:radius="12dp" />
</shape>
ko_switch_track_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E3E3E3" />
<stroke
android:width="0dp"
android:color="#00000000" />
<corners android:radius="12dp" />
</shape>
本站内容来源于作者发布和网络转载,如有版权相关问题请及时与我们取得联系,我们将立即删除。