Android中Switch修改樣式

1.xml中設(shè)置Switch控件

<Switch

? ? android:id="@+id/individuationSwitch"

? ? android:layout_width="40dp"

? ? android:layout_height="20dp"

????android:checked="false"

????android:thumb="@drawable/thumb"

????android:track="@drawable/track"

/>

2.底部滑動條顏色設(shè)置(track)

2.1:? track? ?選擇器

<?xml version="1.0" encoding="utf-8"?><!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態(tài)下,底下下滑條的顏色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

? ? <item android:drawable="@drawable/blue_track" android:state_checked="true" />

? ? <item android:drawable="@drawable/gray_track" />

</selector>

2.2:? blue_track? ?打開狀態(tài)

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

? ? <!-- 高度40 -->

? ? <size android:height="15dp"/>

? ? <!-- 圓角弧度20 -->

? ? <corners android:radius="15dp"/>

? ? <!-- 變化率 -->


? ? ? ? android:endColor="@color/color_6495ED"

? ? ? ? android:startColor="@color/color_6495ED" />

</shape>

2.3:?gray_track? 關(guān)閉狀態(tài)

<?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="15dp"/>

? ? <!-- 圓角弧度15 -->

? ? <corners android:radius="15dp"/>

? ? <!-- 變化率? 定義從左到右的顏色不變 -->


? ? ? ? android:endColor="@color/color_888888"

? ? ? ? android:startColor="@color/color_888888" />

</shape>


3.滑動按鈕顏色設(shè)置(thumb)

3.1:thumb?? 選擇器

<?xml version="1.0" encoding="utf-8"?><!-- 按鈕的選擇器,可以設(shè)置按鈕在不同狀態(tài)下的時候,按鈕不同的顏色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

? ? <item android:drawable="@drawable/blue_thumb" android:state_checked="true" />

? ? <item android:drawable="@drawable/gray_thumb" />

</selector>

3.2:blue_thumb? ?打開狀態(tài)

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:shape="rectangle" >

? ? <!-- 高度40 -->

? ? <size android:height="20dp" android:width="20dp"/>

? ? <!-- 圓角弧度20 -->

? ? <corners android:radius="20dp"/>

? ? <!-- 變化率 -->


? ? ? ? android:endColor="@color/white"

? ? ? ? android:startColor="@color/white" />

? ? <stroke android:width="1dp"

? ? ? ? android:color="@color/color_6495ED"/>

</shape>

3.3:?gray_thumb? 關(guān)閉狀態(tài)

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:shape="rectangle" >

? ? <!-- 高度40 -->

? ? <size android:height="20dp" android:width="20dp"/>

? ? <!-- 圓角弧度20 -->

? ? <corners android:radius="20dp"/>

? ? <!-- 變化率 -->


? ? ? ? android:endColor="@color/white"

? ? ? ? android:startColor="@color/white" />

? ? <stroke android:width="1dp"

? ? ? ? android:color="@color/color_666666"/>

</shape>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容