Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
??自定義Switch外觀
外觀定制這塊屬于基操了,我們利用屬性 android:track 和 android:thumb 定制 Switch 的背景圖片和滑塊圖片,UI那能直接切圖肯定做起來更快,此方式實(shí)現(xiàn)極其簡單指定圖片就行,所以今天我們實(shí)操的是自定義drawable的形式。
布局樣式
<Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/selector_switch_thumb" android:layout_margin="16dp" android:track="@drawable/selector_switch_track" />
Drawable代碼
<?xml version="1.0" encoding="utf-8"?><!--switch的自定義軌道--> <!--selector_switch_track.xml文件--> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/track_on" android:state_checked="true" /> <item android:drawable="@drawable/track_off" android:state_checked="false" /> </selector> <?xml version="1.0" encoding="utf-8"?><!--switch的自定義圓鈕--> <!--selector_switch_thumb.xml文件--> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/thumb_on" android:state_checked="true" /> <item android:drawable="@drawable/thumb_off" android:state_checked="false" /> </selector> <?xml version="1.0" encoding="utf-8"?> <!--track_on.xml文件--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#BB00FF00" /> <!-- 這個(gè)是用來實(shí)現(xiàn)軌道高度小于圓鈕高度的,值越大軌道越細(xì)--> <!-- 同理,若thumb有stroke,track沒有,可實(shí)現(xiàn)圓鈕在軌道里的偽效果--> <stroke android:width="8dp" android:color="#00000000" /> <corners android:radius="20dp" /> </shape> <?xml version="1.0" encoding="utf-8"?> <!--track_off.xml文件--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#E4E4E4" /> <!-- 這個(gè)是用來實(shí)現(xiàn)軌道高度小于圓鈕高度的,值越大軌道越細(xì)--> <stroke android:width="8dp" android:color="#00000000" /> <corners android:radius="20dp" /> </shape> <?xml version="1.0" encoding="utf-8"?> <!--thumb_on.xml文件--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFF00" /> <size android:width="20dp" android:height="20dp" /> </shape> <?xml version="1.0" encoding="utf-8"?> <!--thumb_off.xml文件--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#AAAAAA" /> <size android:width="20dp" android:height="20dp" /> </shape>
要想實(shí)現(xiàn)下圖效果:
就是小空在代碼中注釋所述,在開關(guān)按鈕上增加一個(gè)透明的邊框,軌道的高度會(huì)自動(dòng)變化。
除了Switch還有另一個(gè)開關(guān)ToggleButton,該控件無thumb和track,相比Switch缺少了滑動(dòng)的動(dòng)畫效果。在使用上和Switch基本一致,同樣可以自定義。
以上就是Android開發(fā)手冊自定義Switch開關(guān)按鈕控件的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)自定義Switch控件的資料請關(guān)注腳本之家其它相關(guān)文章!
- Android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式(實(shí)例demo)
- 使用PlatformView將?Android?控件view制作成Flutter插件
- Android開發(fā)自定義實(shí)時(shí)圖表控件實(shí)現(xiàn)示例
- Android開發(fā)手冊TextView控件及陰影效果實(shí)現(xiàn)
- Android開發(fā)自定義雙向SeekBar拖動(dòng)條控件
- Android開發(fā)手冊RatingBar星級評分控件實(shí)例
- Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解
相關(guān)文章
Android項(xiàng)目中使用HTTPS配置的步驟詳解
這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中使用HTTPS配置步驟的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06Android中WebView無法后退和js注入漏洞的解決方案
這篇文章主要介紹了Android中WebView無法后退和js注入漏洞解決方案,其中js注入主要針對安卓4.2及以下版本中WebView的漏洞,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)帶節(jié)點(diǎn)的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶節(jié)點(diǎn)的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android開發(fā)疫情查詢app(實(shí)例代碼)
這篇文章主要介紹了用Android開發(fā)一個(gè)疫情查詢的APP,文中代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Android如何跳轉(zhuǎn)到應(yīng)用商店的APP詳情頁面
最近做項(xiàng)目遇到這樣的需求,要求從App內(nèi)部點(diǎn)擊按鈕或鏈接,跳轉(zhuǎn)到應(yīng)用商店的某個(gè)APP的詳情頁面,怎么實(shí)現(xiàn)此功能呢?下面小編給大家分享Android如何跳轉(zhuǎn)到應(yīng)用商店的APP詳情頁面,需要的朋友參考下2017-01-01詳解如何在Android studio中更新sdk版本和build-tools版本
這篇文章主要介紹了如何在Android studio中更新sdk版本和build-tools版本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android實(shí)現(xiàn)隨手指移動(dòng)小球
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隨手指移動(dòng)小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08