Android UI控件Switch的使用方法
在Android中偶爾會用到開關(guān),Switch就是一個簡單易使用的不錯的控件。
首先,在布局中添加上Switch控件:
<Switch android:id="@+id/s_v" android:layout_width="wrap_content" android:layout_height="wrap_content" android:switchMinWidth="20dp" android:textOn="on" android:textOff="off" android:thumb="@drawable/thumb" android:track="@drawable/track" />
以下是該控件的常用屬性:
textOn:控件打開時顯示的文字
textOff:控件關(guān)閉時顯示的文字
thumb:控件開關(guān)的圖片
track:控件開關(guān)的軌跡圖片
typeface:設(shè)置字體類型
switchMinWidth:開關(guān)最小寬度
switchPadding:設(shè)置開關(guān) 與文字的空白距離
switchTextAppearance:設(shè)置文本的風格
checked:設(shè)置初始選中狀態(tài)
splitTrack:是否設(shè)置一個間隙,讓滑塊與底部圖片分隔(API 21及以上)
showText:設(shè)置是否顯示開關(guān)上的文字(API 21及以上)
我們一般不會用該控件原本的樣式,那么我們就需要自己修改樣式了:
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="40dp" android:width="40dp"/> <!-- 圓角弧度 20 --> <corners android:radius="20dp"/> <!-- 變化率 --> <gradient android:endColor="#ffffff" android:startColor="#ffffff" /> <stroke android:width="1dp" android:color="#9e9e9e"/> </shape>
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="40dp" android:width="40dp"/> <!-- 圓角弧度 20 --> <corners android:radius="20dp"/> <!-- 變化率 --> <gradient android:endColor="#ffffff" android:startColor="#ffffff" /> <stroke android:width="1dp" android:color="#33da33"/> </shape>
gray_track.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 高度 此處設(shè)置寬度無效--> <size android:height="20dp"/> <!-- 圓角弧度 15 --> <corners android:radius="25dp"/> <!-- 變化率 定義從左到右的顏色不變 --> <gradient android:endColor="#9e9e9e" android:startColor="#9e9e9e" /> </shape>
green_track.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 高度40 --> <size android:height="20dp"/> <!-- 圓角弧度 20 --> <corners android:radius="25dp"/> <!-- 變化率 --> <gradient android:endColor="#33da33" android:startColor="#33da33" /> </shape>
thumb.xml:
<?xml version="1.0" encoding="utf-8"?> <!-- 設(shè)置按鈕在不同狀態(tài)下的時候,按鈕不同的顏色 --> <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>
track.xml:
<?xml version="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>
在styles.xml中添加如下style:
<style name="s_true" parent="@android:style/TextAppearance.Small"> <item name="android:textColor">#33da33</item> </style> <style name="s_false" parent="@android:style/TextAppearance.Small"> <item name="android:textColor">#9b9b9b</item> </style>
最后,只需要將控件實例化出來進行相應(yīng)操作就可以了:
MainActivity.class:
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Switch aSwitch = (Switch) findViewById(R.id.s_v); aSwitch.setChecked(false); aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //控制開關(guān)字體顏色 if (b) { aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.s_true); }else { aSwitch.setSwitchTextAppearance(MainActivity.this,R.style.x1); } } }); } }
最終效果如下圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android CheckBox中設(shè)置padding無效解決辦法
- Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例
- Android 中CheckBox多項選擇當前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實例詳解
- Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實例詳解
- Android單選按鈕RadioButton的使用方法
- Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
Android自定義狀態(tài)欄顏色與APP風格保持一致的實現(xiàn)方法
我們知道iOS上的應(yīng)用,狀態(tài)欄的顏色總能與應(yīng)用標題欄顏色保持一致,用戶體驗很不錯,那安卓是否可以呢?下面小編給大家?guī)砹薃ndroid自定義狀態(tài)欄顏色與APP風格保持一致的實現(xiàn)方法,跟著小編一起學(xué)習(xí)吧2016-10-10深入解析Android中View創(chuàng)建的全過程
這篇文章主要給大家深入的解析了關(guān)于Android中View創(chuàng)建的全過程,文中介紹的非常詳細,相信對大家會有一定的參考借鑒,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-03-03如何設(shè)置Android studio 3.0顯示光標返回上一次瀏覽位置的箭頭圖標
這篇文章主要介紹了如何設(shè)置Android studio 3.0顯示光標返回上一次瀏覽位置的箭頭圖標 很多朋友反映剛升級了Android studio 3.0,發(fā)現(xiàn)光標返回上一次瀏覽位置的箭頭圖標沒有了,下文給大家介紹的非常詳細,需要的朋友可以參考下2017-11-11Android實現(xiàn)開機自動啟動Service或app的方法
這篇文章主要介紹了Android實現(xiàn)開機自動啟動Service或app的方法,結(jié)合實例形式分析了Android開機自啟動程序的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-07-07