接入百度人臉的demo時候,發現了內置的switch開關比較好看,看了下實現方法,原來只是改了下樣式,記錄一下
效果:
代碼
<Switch
android:id="@+id/announcements_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:textOff=""
android:textOn=""
android:checked="false"
android:thumb="@drawable/setting_switch_thumb"
android:track="@drawable/setting_switch_track_selector" />
setting_switch_thumb
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF" />
<stroke
android:width="3dp"
android:color="#00000000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
setting_switch_track_selector
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/setting_switch_track_on" android:state_checked="true" />
<item android:drawable="@drawable/setting_switch_track_off" android:state_checked="false" />
</selector>
setting_switch_track_on
和setting_switch_track_off
只是顏色不同,setting_switch_track_off
就不寫了
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00BAF2" />
<corners android:radius="20dp" />
</shape>