Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
??自定義Switch外觀
外觀定制這塊屬于基操了,我們利用屬性 android:track 和 android:thumb 定制 Switch 的背景圖片和滑塊圖片,UI那能直接切圖肯定做起來更快,此方式實現(xiàn)極其簡單指定圖片就行,所以今天我們實操的是自定義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" />
<!-- 這個是用來實現(xiàn)軌道高度小于圓鈕高度的,值越大軌道越細-->
<!-- 同理,若thumb有stroke,track沒有,可實現(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" />
<!-- 這個是用來實現(xiàn)軌道高度小于圓鈕高度的,值越大軌道越細-->
<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>
要想實現(xiàn)下圖效果:

就是小空在代碼中注釋所述,在開關(guān)按鈕上增加一個透明的邊框,軌道的高度會自動變化。
除了Switch還有另一個開關(guān)ToggleButton,該控件無thumb和track,相比Switch缺少了滑動的動畫效果。在使用上和Switch基本一致,同樣可以自定義。

以上就是Android開發(fā)手冊自定義Switch開關(guān)按鈕控件的詳細內(nèi)容,更多關(guān)于Android開發(fā)自定義Switch控件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中WebView無法后退和js注入漏洞的解決方案
這篇文章主要介紹了Android中WebView無法后退和js注入漏洞解決方案,其中js注入主要針對安卓4.2及以下版本中WebView的漏洞,需要的朋友可以參考下2016-02-02
Android如何跳轉(zhuǎn)到應(yīng)用商店的APP詳情頁面
最近做項目遇到這樣的需求,要求從App內(nèi)部點擊按鈕或鏈接,跳轉(zhuǎn)到應(yīng)用商店的某個APP的詳情頁面,怎么實現(xiàn)此功能呢?下面小編給大家分享Android如何跳轉(zhuǎn)到應(yīng)用商店的APP詳情頁面,需要的朋友參考下2017-01-01
詳解如何在Android studio中更新sdk版本和build-tools版本
這篇文章主要介紹了如何在Android studio中更新sdk版本和build-tools版本,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

