網站首頁 編程語言 正文
??自定義Switch外觀
外觀定制這塊屬于基操了,我們利用屬性 android:track 和 android:thumb 定制 Switch 的背景圖片和滑塊圖片,UI那能直接切圖肯定做起來更快,此方式實現極其簡單指定圖片就行,所以今天我們實操的是自定義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" /> <!-- 這個是用來實現軌道高度小于圓鈕高度的,值越大軌道越細--> <!-- 同理,若thumb有stroke,track沒有,可實現圓鈕在軌道里的偽效果--> <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" /> <!-- 這個是用來實現軌道高度小于圓鈕高度的,值越大軌道越細--> <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>
要想實現下圖效果:
就是小空在代碼中注釋所述,在開關按鈕上增加一個透明的邊框,軌道的高度會自動變化。
除了Switch還有另一個開關ToggleButton,該控件無thumb和track,相比Switch缺少了滑動的動畫效果。在使用上和Switch基本一致,同樣可以自定義。
原文鏈接:https://juejin.cn/post/7063324477907533860
相關推薦
- 2022-10-23 C#位運算符的基本用法介紹_C#教程
- 2023-01-03 python實現線性插值的示例_python
- 2022-04-16 pycharm實現設置自動的參數注釋標識_python
- 2022-05-11 C#實現漢字轉漢語拼音的示例代碼_C#教程
- 2022-10-04 goland把go項目打包進docker鏡像的全過程記錄_Golang
- 2022-05-23 如何使Python中的print()語句運行結果不換行_python
- 2022-05-19 詳解QTreeWidget隱藏節點的兩種方式_C 語言
- 2022-06-17 Go基礎教程系列之WaitGroup用法實例詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支