官网文档:https://developer.android.google.cn/reference/androidx/appcompat/widget/SwitchCompat
属性介绍:https://www.jianshu.com/p/796e2f7f0ed2
实现效果:
布局xml:
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvTitle"
android:layout_alignParentRight="true"
android:textOff="繁"
android:textOn="简"
android:thumb="@drawable/switch_ios_thumb"
app:showText="true"
app:switchTextAppearance="@style/switchStyle"
app:track="@drawable/switch_ios_track_selector" />
drawable新建文件: switch_ios_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFF" />
<stroke
android:width="3dp"
android:color="#00000000" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
switch_ios_track_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_ios_track_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_ios_track_off" android:state_checked="false" />
</selector>
switch_ios_track_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="#FD751F" />
<corners android:radius="25dp"/>
</shape>
switch_ios_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="#E3E3E3" />
<corners android:radius="25dp" />
</shape>
style.xml中添加switchStyle:
<style name="switchStyle" parent="Theme.AppCompat.Light">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#000000</item>
</style>