0
点赞
收藏
分享

微信扫一扫

安卓自定义Switch开关控件

追风骚年 2022-02-22 阅读 72


实现效果

安卓自定义Switch开关控件_控件

实现方案

  1. 背景: switch_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_selected_track" android:state_checked="true" />
<item android:drawable="@drawable/switch_unselected_track" />
</selector>
  1. 滑块: switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- 滑动块的宽高 -->
<size
android:width="20dp"
android:height="20dp" />
<solid android:color="#FFFFFF" />
<!-- 透明的描边-->
<stroke
android:width="3dp"
android:color="@android:color/transparent" />
</shape>
  1. 选中背景:switch_selected_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="20dp" />
<corners android:radius="10dp" />
<solid android:color="#00C653" />
</shape>
  1. 未选中背景: switch_unselected_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:height="20dp"/>
<corners android:radius="10dp"/>
<solid android:color="#D9D9D9" />
</shape>
  1. 使用方法
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="20dp"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_track" />




举报

相关推荐

0 条评论