實(shí)現(xiàn)效果如圖所示:
相關(guān)屬性?
android:showText:設(shè)置on/off的時(shí)候是否顯示文字,boolean?
android:splitTrack:是否設(shè)置一個(gè)間隙秀又,讓滑塊與底部圖片分隔,boolean?
android:switchMinWidth:設(shè)置開(kāi)關(guān)的最小寬度?
android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔?
android:switchTextAppearance:設(shè)置開(kāi)關(guān)的文字外觀吐辙,暫時(shí)沒(méi)發(fā)現(xiàn)有什么用…?
android:textOff:按鈕沒(méi)有被選中時(shí)顯示的文字?
android:textOn:按鈕被選中時(shí)顯示的文字?
android:textStyle:文字風(fēng)格蘸劈,粗體,斜體寫(xiě)劃線那些?
android:track:底部的圖片?
android:thumb:滑塊的圖片?
1.在代碼中布局:
<Switch
????android:id="@+id/switch_fingerline"
? ? android:layout_height="wrap_content"
? ? android:layout_width="wrap_content"
? ? android:checked="true"
? ? android:thumb="@drawable/thumb_selector"
? ? android:track="@drawable/track_selector"
? ? android:theme="@style/SwitchCompat.Control"/>?
2.按鈕樣式
thumb_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? <item android:drawable="@drawable/thumb_on" android:state_checked="true"/>
? ? <item android:drawable="@drawable/thumb_off" android:state_checked="false"/>
</selector>
thumb_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="oval">
? ? ? ? android:width="5dp"
? ? ? ? android:color="@android:color/transparent" />
? ? <solid android:color="@android:color/white" />
? ? ? ? android:width="20dp"
? ? ? ? android:height="20dp" />
</shape>
thumb_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
? ? ? ? android:width="5dp"
? ? ? ? android:color="@android:color/transparent" />
? ? <solid android:color="@android:color/white" />
? ? ? ? android:width="20dp"
? ? ? ? android:height="20dp" />
</shape>
track_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? <item android:drawable="@drawable/track_on" android:state_checked="true"/>
? ? <item android:drawable="@drawable/track_off" android:state_checked="false"/>
</selector>
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/maincolor" />
? ? <corners android:radius="20dp"/>
</shape>
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="#AAA" />
? ? <corners android:radius="20dp" />
</shape>
3.開(kāi)關(guān)監(jiān)控回調(diào):
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
????@Override
? ? ????public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
????????????if(!buttonView.isPressed()){?//?每次 setChecked 時(shí)會(huì)觸發(fā)onCheckedChanged 監(jiān)聽(tīng)回調(diào),而有時(shí)我們?cè)谠O(shè)置setChecked后不想去自動(dòng)觸發(fā) onCheckedChanged 里的具體操作, 即想屏蔽掉onCheckedChanged;加上此判斷
????????????????????return;
? ? ? ? ? ? ?}
????????}
});
--------END------
我是靜染星辰孵构,私人微信:azxy986753
歡迎添加微信烟很,互相學(xué)習(xí),互相進(jìn)步恤筛!