最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件

 更新時(shí)間:2022年06月10日 16:47:47   作者:芝麻粒兒  
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

??自定義Switch外觀

外觀定制這塊屬于基操了,我們利用屬性 android:track 和 android:thumb 定制 Switch 的背景圖片和滑塊圖片,UI那能直接切圖肯定做起來(lái)更快,此方式實(shí)現(xiàn)極其簡(jiǎ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è)是用來(lái)實(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è)是用來(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)下圖效果:

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

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

以上就是Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)自定義Switch控件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

介休市| 衡山县| 通渭县| 湘阴县| 五河县| 民和| 格尔木市| 云和县| 文昌市| 龙泉市| 华蓥市| 丽江市| 右玉县| 图木舒克市| 南岸区| 阿克苏市| 土默特左旗| 桓仁| 托克逊县| 阿巴嘎旗| 南华县| 潍坊市| 温宿县| 浮梁县| 永城市| 宕昌县| 永胜县| 咸丰县| 祁连县| 安达市| 宁陵县| 三都| 始兴县| 镇远县| 个旧市| 神木县| 怀化市| 右玉县| 富宁县| 清镇市| 拜泉县|