注:本文為轉載,原文鏈接
Android中自帶的Switch控件在很多時候總覺得和整體系統風格不符,很多時候,自定義Switch是一種方法。
但其實不用這么麻煩,安卓自帶的Switch通過修改一些屬性,也可以達到和自定義Switch差不多的一個效果。
個人感覺,Switch的屬性設置和其他控件還是有挺大區別的。因此,寫下此文,方便有需要的同學參考。
先上效果圖:
以上便是修改后效果 與 原生Switch的效果對比。代碼在文章底部給出
實現方式:
1.底部滑動條,在開關打開狀態為綠色,開關關閉狀態為灰色
在 res/drawable 文件夾下面,寫兩個滑動條的底圖 ,通過一個選擇器selector進行控制。
gray_track.xml :非打開狀態,灰色的底圖
<?xmlversion="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--高度30此處設置寬度無效-->
<size android:height="30dp"/>
<!--圓角弧度15-->
<corners android:radius="15dp"/>
<!--變化率定義從左到右的顏色不變-->
<gradient
android:endColor="#888888"
android:startColor="#888888"/>
</shape>
green_track.xml:打開狀態下,綠色的底圖。
<?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不同狀態下,滑動條的底圖
<?xmlversion="1.0"encoding="utf-8"?>
<!--底層下滑條的樣式選擇器,可控制Switch在不同狀態下,底下下滑條的顏色-->
<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. 滑動按鈕:底色我用的接近白色的淡灰色,打開時,邊上的一圈線條為灰色,關閉時,邊上的一圈線條為綠色
實現方式和底部滑動一致
gray_thumb.xml :關閉狀態,按鈕邊上一圈顏色為深灰色
<?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 : 打開狀態,按鈕邊上一圈的顏色為綠色
<?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不同狀態下,按鈕的顯示狀態
<?xmlversion="1.0"encoding="utf-8"?>
<!--按鈕的選擇器,可以設置按鈕在不同狀態下的時候,按鈕不同的顏色-->
<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. 將以上選擇器設置給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" />
<!--用于對比使用的不設置任何屬性的Switch-->
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4.高度,寬度的設置
細心的同學會發現,修改 android:layout_width , android:layout_height 這兩個屬性,并不會實際修改Switch的大小.
設置大了,邊上會出現空白部分,設置小了,Switch顯示不全。
實際設置高度方法:
上面定義滑動條和按鈕底圖的地方相信大家都注意到,<size android:height="30dp"/>
這行代碼,
修改 green_track.xml,gray_track.xml 中的高度,即可修改高度(修改green_thumb.xml gray_thumb.xml 中的高度貌似無效)。
實際修改寬度的方法:
(1)修改滑動按鈕的寬度:滑動按鈕的寬度和按鈕上的文字有關,
想要按鈕變長,在按鈕顯示的文字上添加幾個空字符串即可,想要按鈕變短的話,減少按鈕上顯示的字即可(修改按鈕上字體大小也可以試試)
Switch的屬性
android:textOn=" "
android:textOff=" "
(2)修改按鈕 打開,關閉 兩種狀態之間滑動距離(貌似小到一定程度,再改小就無效了)
Switch的屬性
android:switchMinWidth="20dp"
通過以上的設置,相信能滿足大部分實際使用的需求了,希望對大家有幫助。
下載地址:http://download.csdn.net/detail/qq_34763699/9751234
百度網盤:http://pan.baidu.com/s/1slp1CZV