網站首頁 編程語言 正文
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-01-10 Layui碰上Thymeleaf的解析問題
- 2022-04-12 C++制作簡單的計算器功能_C 語言
- 2022-08-20 Django細致講解多對多使用through自定義中間表方法_python
- 2022-03-30 Android?Room數據庫加密詳解_Android
- 2022-09-01 一文搞懂Scrapy與MongoDB交互過程_MongoDB
- 2024-03-18 JDK版本對應其bytecode version (major version)
- 2022-07-11 Android?Studio實現注冊頁面跳轉登錄頁面的創建_Android
- 2022-11-23 iOS開發學習?ViewController使用示例詳解_IOS
- 最近更新
-
- 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同步修改后的遠程分支