2022-07-25 修改系统开关SwitchCompat显示效果

1.效果如下:

关闭效果.png

开启效果.png

2.相关XML文件:

a)switch_common_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="@color/white" />
  <size
      android:width="20dp"
      android:height="20dp" />
  <stroke
      android:width="6dp"
      android:color="#00000000" />
  <corners android:radius="1000dp" />
</shape>

b)switch_common_track_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="@color/color_333" />
  <corners android:radius="1000dp" />
</shape>

c)switch_common_track_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="@color/color_DCDCDC" />
  <corners android:radius="1000dp" />
</shape>

d)switch_common_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/switch_common_track_selected" android:state_checked="true" />
  <item android:drawable="@drawable/switch_common_track_unselected" android:state_checked="false" />
</selector>

e)SwitchCompat使用代码

  <androidx.appcompat.widget.SwitchCompat
      android:id="@+id/switchView"
      android:layout_width="40dp"
      android:layout_height="24dp"
      android:layout_gravity="center_vertical|end"
      android:layout_marginEnd="21dp"
      android:thumb="@drawable/switch_common_thumb"
      app:switchMinWidth="40dp"
      app:track="@drawable/switch_common_track" />
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容