Android Swtich開關樣式調整

原文:Android Swtich開關樣式調整 - Stars-One的雜貨小窩

接入百度人臉的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_onsetting_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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章