首先添加控件:
<Switch
? ? android:id="@+id/sw_sfktmmzf"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_marginRight="15dp"
? ? android:showText="false"
? ? android:switchMinWidth="50dp"
? ? android:thumb="@drawable/thumb"
? ? android:track="@drawable/track" />
以下是該控件的常用屬性:
textOn:控件打開時顯示的文字
textOff:控件關(guān)閉時顯示的文字
thumb:控件開關(guān)的圖片(設(shè)置小圓圈顏色)
track:控件開關(guān)的軌跡圖片(設(shè)置小圓圈背景顏色)
typeface:設(shè)置字體類型
switchMinWidth:開關(guān)最小寬度
switchPadding:設(shè)置開關(guān) 與文字的空白距離
switchTextAppearance:設(shè)置文本的風(fēng)格
checked:設(shè)置初始選中狀態(tài)
splitTrack:是否設(shè)置一個間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設(shè)置是否顯示開關(guān)上的文字(API 21及以上)
創(chuàng)建北京控制文件在drawable文件下
1、thumb.xml
<?xml version="1.0" encoding="utf-8"?><!-- 按鈕的選擇器,可以設(shè)置按鈕在不同狀態(tài)下的時候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? <item android:drawable="@drawable/green_thumb" android:state_checked="true" />
? ? <item android:drawable="@drawable/gray_thumb" />
顏色文件:
green_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle" >
? ? <!-- 高度40 -->
? ? <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>
? ? <!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
? ? <gradient
? ? ? ? android:endColor="#eeeeee"
? ? ? ? android:startColor="#eeeeee" />
<stroke android:width="1dp"
? ? ? ? android:color="@color/home_text1"/>
</shape>
gray_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle" >
? ? <!-- 高度40 -->
? ? <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>
? ? <!-- 圓角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 變化率 -->
? ? <gradient
? ? ? ? android:endColor="#eeeeee"
? ? ? ? android:startColor="#eeeeee" />
? ? <stroke android:width="1dp"
? ? ? ? android:color="@color/text_color03"/>
</shape>
2、track.xml
<?xml version="1.0" encoding="utf-8"?><!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態(tài)下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? <item android:drawable="@drawable/green_track" android:state_checked="true" />
? ? <item android:drawable="@drawable/gray_track" />
</selector>
顏色文件:
green_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle">
? ? <!-- 高度40 -->
? ? <size android:height="@dimen/switch_height"/>
? ? <!-- 圓角弧度 20 -->
? ? <corners android:radius="15dp"/>
? ? <!-- 變化率 -->
? ? <gradient
? ? ? ? android:endColor="@color/home_text1"
? ? ? ? android:startColor="@color/home_text1" />
</shape>
gray_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle">
? ? <!-- 高度30? 此處設(shè)置寬度無效-->
? ? <size android:height="@dimen/switch_height" />
? ? <!-- 圓角弧度 15 -->
? ? <corners android:radius="15dp" />
? ? <!-- 變化率? 定義從左到右的顏色不變 -->
? ? <gradient
? ? ? ? android:endColor="@color/text_color03"
? ? ? ? android:startColor="@color/text_color03" />
</shape>
switch 控件監(jiān)聽事件:
?aSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
??????@Override
????public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
????????//控制開關(guān)字體顏色
????????if(isChecked) {
???????? //打開
????????}else{
???????? //關(guān)閉
????????}
??????}
????});