溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android如何自定義Switch開(kāi)關(guān)按鈕控件

發(fā)布時(shí)間:2022-06-13 14:13:54 來(lái)源:億速云 閱讀:347 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇“Android如何自定義Switch開(kāi)關(guān)按鈕控件”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Android如何自定義Switch開(kāi)關(guān)按鈕控件”文章吧。

自定義Switch外觀

外觀定制這塊屬于基操了,我們利用屬性 android:track 和 android:thumb 定制 Switch 的背景圖片和滑塊圖片,UI那能直接切圖肯定做起來(lái)更快,此方式實(shí)現(xiàn)極其簡(jiǎn)單指定圖片就行。

Android如何自定義Switch開(kāi)關(guān)按鈕控件

布局樣式

<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è)是用來(lái)實(shí)現(xiàn)軌道高度小于圓鈕高度的,值越大軌道越細(xì)-->
    <!-- 同理,若thumb有stroke,track沒(méi)有,可實(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è)是用來(lái)實(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)下圖效果:

Android如何自定義Switch開(kāi)關(guān)按鈕控件

就是小空在代碼中注釋所述,在開(kāi)關(guān)按鈕上增加一個(gè)透明的邊框,軌道的高度會(huì)自動(dòng)變化。

除了Switch還有另一個(gè)開(kāi)關(guān)ToggleButton,該控件無(wú)thumb和track,相比Switch缺少了滑動(dòng)的動(dòng)畫(huà)效果。在使用上和Switch基本一致,同樣可以自定義。

Android如何自定義Switch開(kāi)關(guān)按鈕控件

以上就是關(guān)于“Android如何自定義Switch開(kāi)關(guān)按鈕控件”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI