当前位置:网站首页>Custom switch control
Custom switch control
2022-04-23 04:44:00 【Vivid_ Mm】
Need to be in drawable Create four new directories xml file
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>
The first two are switches with movable sliders xml file , The last two are for the slider track xml file , Next, create another xml file 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>
This xml The document specifies the corresponding parameters for different state references of the slider xml file , There are two ways :
Method 1. stay value-->styles.xml Add
style name="My.TextAppearance" parent="@style/TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">20dp</item>
<item name="android:textColor">#000</item>
</style>
Then in your layout xml Add... To the file :
<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" />
Running simulator , You can see the custom switch.
Method 2. Let's create another one xml file ,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>
Then go to the layout file reference switch Control
<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=" open "
android:textOff=" Turn off "
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track"
tools:checked="true" />
Here's the moving track ahead xml in
<stroke android:color="#00000000"
android:width="20dp" />
Represents using a transparent border to constrain the track inside, so as to achieve , The track is a thin line , I would like to thank my colleagues for their guidance @jerome
版权声明
本文为[Vivid_ Mm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558084568.html
边栏推荐
- Coinbase:关于跨链桥的基础知识、事实和统计数据
- What is the thirty-six plan
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- 补:注解(Annotation)
- Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
- What's the difference between error and exception
- Case of using stream load to write data to Doris
- 補:注解(Annotation)
- Leetcode002 -- inverts the numeric portion of a signed integer
- MySQL - data read / write separation, multi instance
猜你喜欢
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Coinbase: basic knowledge, facts and statistics about cross chain bridge
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
Innovative practice of short video content understanding and generation technology in meituan
Installation and deployment of Flink and wordcount test
補:注解(Annotation)
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
Brushless motor drive scheme based on Infineon MCU GTM module
Unity rawimage background seamlessly connected mobile
Apache Bench(ab 压力测试工具)的安装与使用
随机推荐
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
SQL statement for adding columns in MySQL table
win10, mysql-8.0.26-winx64. Zip installation
leetcode008--实现strStr()函数
Interaction of diet gut microbiota on cardiovascular disease
C language: Advanced pointer
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Leetcode001 -- returns the subscript of the array element whose sum is target
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
leetcode006--查找字符串数组中的最长公共前缀
Spark case - wordcount
229. Find mode II
補:注解(Annotation)
Error occurs when thymeleaf th: value is null
Record the blind injection script
Basic operation of sequence table
Leetcode008 -- implement strstr() function
No such file or directory problem while executing shell