注:本文為轉(zhuǎn)載,原文鏈接
Android中自帶的Switch控件在很多時(shí)候總覺得和整體系統(tǒng)風(fēng)格不符哮洽,很多時(shí)候纹磺,自定義Switch是一種方法。
但其實(shí)不用這么麻煩钮惠,安卓自帶的Switch通過修改一些屬性茅糜,也可以達(dá)到和自定義Switch差不多的一個(gè)效果。
個(gè)人感覺素挽,Switch的屬性設(shè)置和其他控件還是有挺大區(qū)別的蔑赘。因此,寫下此文预明,方便有需要的同學(xué)參考缩赛。
先上效果圖:
以上便是修改后效果 與 原生Switch的效果對比。代碼在文章底部給出
實(shí)現(xiàn)方式:
1.底部滑動條贮庞,在開關(guān)打開狀態(tài)為綠色,開關(guān)關(guān)閉狀態(tài)為灰色
在 res/drawable 文件夾下面究西,寫兩個(gè)滑動條的底圖 窗慎,通過一個(gè)選擇器selector進(jìn)行控制。
gray_track.xml :非打開狀態(tài)卤材,灰色的底圖
<?xmlversion="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--高度30此處設(shè)置寬度無效-->
<size android:height="30dp"/>
<!--圓角弧度15-->
<corners android:radius="15dp"/>
<!--變化率定義從左到右的顏色不變-->
<gradient
android:endColor="#888888"
android:startColor="#888888"/>
</shape>
green_track.xml:打開狀態(tài)下遮斥,綠色的底圖。
<?xmlversion="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--高度40-->
<size android:height="30dp"/>
<!--圓角弧度20-->
<corner sandroid:radius="15dp"/>
<!--變化率-->
<gradient
android:endColor="#33da33"
android:startColor="#33da33"/>
</shape>
選擇器 track.xml 用于控制Switch不同狀態(tài)下扇丛,滑動條的底圖
<?xmlversion="1.0"encoding="utf-8"?>
<!--底層下滑條的樣式選擇器术吗,可控制Switch在不同狀態(tài)下,底下下滑條的顏色-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item android:drawable="@drawable/gray_track" />
</selector>
2. 滑動按鈕:底色我用的接近白色的淡灰色帆精,打開時(shí)较屿,邊上的一圈線條為灰色,關(guān)閉時(shí)卓练,邊上的一圈線條為綠色
實(shí)現(xiàn)方式和底部滑動一致
gray_thumb.xml :關(guān)閉狀態(tài)隘蝎,按鈕邊上一圈顏色為深灰色
<?xmlversion="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--高度40-->
<size android:height="40dp" android:width="40dp" />
<!--圓角弧度20-->
<corners android:radius="20dp"/>
<!--變化率-->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#666666" />
</shape>
green_thumb.xml : 打開狀態(tài),按鈕邊上一圈的顏色為綠色
<?xmlversion="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--高度40-->
<size android:height="40dp"android:width="40dp"/>
<!--圓角弧度20-->
<corners android:radius="20dp"/>
<!--變化率-->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee"/>
<stroke android:width="1dp"
android:color="#33da33"/>
</shape>
選擇器 thumb.xml 用于控制Switch不同狀態(tài)下襟企,按鈕的顯示狀態(tài)
<?xmlversion="1.0"encoding="utf-8"?>
<!--按鈕的選擇器嘱么,可以設(shè)置按鈕在不同狀態(tài)下的時(shí)候,按鈕不同的顏色-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/green_thumb"/>
<item android:drawable="@drawable/gray_thumb"/>
</selector>
3. 將以上選擇器設(shè)置給Switch顽悼,就好了
界面 activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="20dp"
android:textOn=""
android:textOff=""
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
<!--用于對比使用的不設(shè)置任何屬性的Switch-->
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4.高度曼振,寬度的設(shè)置
細(xì)心的同學(xué)會發(fā)現(xiàn)几迄,修改 android:layout_width , android:layout_height 這兩個(gè)屬性冰评,并不會實(shí)際修改Switch的大小.
設(shè)置大了映胁,邊上會出現(xiàn)空白部分,設(shè)置小了集索,Switch顯示不全屿愚。
實(shí)際設(shè)置高度方法:
上面定義滑動條和按鈕底圖的地方相信大家都注意到,<size android:height="30dp"/>
這行代碼务荆,
修改 green_track.xml,gray_track.xml 中的高度妆距,即可修改高度(修改green_thumb.xml gray_thumb.xml 中的高度貌似無效)。
實(shí)際修改寬度的方法:
(1)修改滑動按鈕的寬度:滑動按鈕的寬度和按鈕上的文字有關(guān)函匕,
想要按鈕變長娱据,在按鈕顯示的文字上添加幾個(gè)空字符串即可,想要按鈕變短的話盅惜,減少按鈕上顯示的字即可(修改按鈕上字體大小也可以試試)
Switch的屬性
android:textOn=" "
android:textOff=" "
(2)修改按鈕 打開中剩,關(guān)閉 兩種狀態(tài)之間滑動距離(貌似小到一定程度,再改小就無效了)
Switch的屬性
android:switchMinWidth="20dp"
通過以上的設(shè)置抒寂,相信能滿足大部分實(shí)際使用的需求了结啼,希望對大家有幫助。
下載地址:http://download.csdn.net/detail/qq_34763699/9751234
百度網(wǎng)盤:http://pan.baidu.com/s/1slp1CZV