網站首頁 編程語言 正文
Switch 組件在日常生常中比較常用,尤其是在配置頁面
網上也有很多自定義的例子,主要是原生的太。。。;
用圖片,在適配中,經常出現拉伸變形的情況, 試著用一下強大的shape
話不多說,上代碼
UI xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="@dimen/item_layout_margin_Top"
android:layout_marginBottom="@dimen/item_layout_margin_left_Top"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/settings_close_back_camera"
android:textColor="@color/settings_text_color"
android:textSize="@dimen/setting_content_textsize" />
<Switch
android:id="@+id/settings_closeBackCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
android:switchMinWidth="@dimen/item_switchMinWidth"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</RelativeLayout>
其中以下標紅地方為重要的樣式配置項
<Switch
android:id="@+id/settings_closeBackCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
android:switchMinWidth="@dimen/item_switchMinWidth"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 按鈕的選擇器,可以設置按鈕在不同狀態下的時候,按鈕不同的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_thumb" />
<item android:drawable="@drawable/gray_thumb" />
</selector>
green_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="@dimen/item_switchSelectwidth" android:width="@dimen/item_switchSelectwidth"/>
<!-- 圓角弧度 20 -->
<corners android:radius="@dimen/item_switchSelectRadius"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#415ff4"/>
</shape>
gray_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="@dimen/item_switchSelectwidth" android:width="@dimen/item_switchSelectwidth"/>
<!-- 圓角弧度 20 -->
<corners android:radius="@dimen/item_switchSelectRadius"/>
<!-- 變化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#666666"/>
</shape>
第二個軌道配置
switch_track.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 底層下滑條的樣式選擇器,可控制Switch在不同狀態下,底下下滑條的顏色 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/green_track" />
<item android:drawable="@drawable/gray_track" />
</selector>
green_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 高度40 -->
<size android:height="30dp"/>
<!-- 圓角弧度 20 -->
<corners android:radius="15dp"/>
<!-- 變化率 -->
<gradient
android:endColor="#415ff4"
android:startColor="#415ff4" />
<stroke android:width="1dp"
android:color="#fff"/>
</shape>
green_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度30 此處設置寬度無效-->
<size android:height="30dp"/>
<!-- 圓角弧度 15 -->
<corners android:radius="15dp"/>
<!-- 做透明 -->
<!-- 變化率 定義從左到右的顏色不變 -->
<!-- <gradient
android:endColor="#888888"
android:startColor="#888888" />-->
<stroke android:width="1dp"
android:color="#fff"/>
</shape>
?
大家有什么好的案例也可以拿出來分享一下
原文鏈接:https://blog.csdn.net/m0_37622302/article/details/119060035
相關推薦
- 2022-12-07 C語言如何求整數的位數及各位數字之和_C 語言
- 2022-03-07 深入淺析C/C++語言結構體指針的使用注意事項_C 語言
- 2024-03-07 SpringAOP基于XML方式實現(了解)
- 2023-11-18 Linux/Ubuntu/Arm設備中通過/proc/stat等文件計算Cpu使用率
- 2022-11-04 深入了解Python中Lambda函數的用法_python
- 2022-11-09 grep正則表達式匹配中括號的方法實例_正則表達式
- 2022-10-11 兩臺主機的vmware虛擬機相互連通的實現步驟_VMware
- 2022-10-24 C語言控制進程之進程等待詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支