当前位置:网站首页>自定义switch控件
自定义switch控件
2022-04-22 05:59:00 【Vivid_Mm】
需要在drawable目录下新建四个xml文件
switch_thumb_on.xml
-
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@android:color/holo_green_dark"> </solid> <size android:width="30dp" android:height="30dp"> </size> <corners android:radius="32dp"> </corners> </shape>switch_thumb_off.xml
-
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#7c7c7c"> </solid> <size android:width="30dp" android:height="30dp"> </size> <corners android:radius="32dp"> </corners> </shape>switch_track_off.xml
-
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/darker_gray"> </solid> <stroke android:color="#00000000" android:width="20dp" /> <corners android:radius="20dp"> </corners> </shape>switch_teack_on.xml
-
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/holo_green_light"> </solid> <stroke android:color="#00000000" android:width="20dp"/> <corners android:radius="20dp"> </corners> </shape>前两个为可移动滑块的开关xml文件,后面两个为滑块轨迹的xml文件,接下来再新建一个xml文件switch_track.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/switch_track_on" />
<item android:state_checked="false"
android:drawable="@drawable/switch_track_off"/>
</selector>
这个xml文件规定了滑块不同状态引用对应的xml文件,此时有两种做法:
方法1. 在value-->styles.xml中添加
style name="My.TextAppearance" parent="@style/TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">20dp</item>
<item name="android:textColor">#000</item>
</style>
然后在你布局的xml文件中添加:
<Switch
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginStart="348dp"
android:layout_weight="1"
android:checked="false"
android:switchTextAppearance="@style/My.TextAppearance"
tools:checked="true" />
运行模拟器,就可以看到自定义switch。
方法2.再新建一个xml文件,switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<item android:state_checked="true"
android:drawable="@drawable/switch_thumb_on"/>
<item android:state_checked="false"
android:drawable="@drawable/switch_thumb_off"/>
</selector>
再去布局文件引用switch控件即可
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="248dp"
android:switchMinWidth="70dp"
android:checked="false"
android:showText="true"
android:textOn="开"
android:textOff="关"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
tools:checked="true" />
这里前面的移动轨迹xml中
<stroke android:color="#00000000"
android:width="20dp" />
代表用一个透明边框去约束里面的轨迹从而实现,轨迹为一条细线,这里感谢我的同事对我的指导@jerome
版权声明
本文为[Vivid_Mm]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xxwbwm/article/details/88305610
边栏推荐
- PostgreSQL 13.3, 12.7, 11.12, 10.17 and 9.6.22 have been released!
- 微信小程序接口封装(uniapp)
- Canvas和SVG的区别
- 创新实训(三)进度
- Differences between OLAP and OLTP and corresponding models (basic knowledge)
- ArcGIS 纵断面分析
- A set of SQL statements supports both Oracle and MySQL?
- 利用win自带功能让处于同一局域网的两个电脑之间互传文件(速度和本地磁盘间互传相同)
- api集中管理
- ArcGIS 观景点视域分析
猜你喜欢

MySQL——索引

将Chrome浏览器背景设置为护眼色(眼睛护航/darkReader插件)

8张图让你一步步看清 async/await 和 promise 的执行顺序

EXCEL IFS函数的使用

Pixel系列无痛升级

Redis的安装启动,菜鸟使用(windows)

The installation of redis is started and used by rookies (Windows)

uniapp生成canvas商品海报

Escort · the future 𞓜 digital risk control summit of digital America 2022 officially set off

Wechat applet uses color font icon (transcript)
随机推荐
nodejs+Express+mongodb
Pgdoucer best practices: a series
LeetCode 1547:切棍子的最小成本
COM in Wine(2)——基本代码分析
js对url进行编码和解码(三种方式区别)
区间列表的交集
JS 获取屏幕,浏览器,网页高度宽度
List&lt;Map&gt;复制:浅拷贝与深拷贝
毕业设计碎碎念
Waterfall layout JS positioning
Pgbackrest practice
纯js链式动画和同时运动
js,jq单行文字上下滚动
创新实训(四)进度
知识点整理:es6
redis存入数据显示乱码问题
EXCEL VLOOKUP函数的使用
How can enterprise risk control build four systems to achieve overall prevention and control?
Redis的安装启动,菜鸟使用(windows)
封装uni网络请求(uni.request)