美文网首页
Switch使用之Shape更改样式

Switch使用之Shape更改样式

作者: gu_jingli | 来源:发表于2020-03-16 12:21 被阅读0次

更改系统Switch样式:只要是thumb(圆形前景开关)和track(长圆背景)属性。

示例代码

1.使用

  <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:thumb="@drawable/shape_thumb"
        android:track="@drawable/shape_track"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_content" />

2.track

<!--drawable/shape_track-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="20dp"
        android:height="10dp" />
    <corners android:radius="10dp" />
    <stroke
        android:width="6dp"
        android:color="#00000000" />

    <solid android:color="#CFCFCF" />
</shape>

3.thumb

<!--drawable/shape_thumb-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size android:width="15dp" />
    <size android:height="15dp" />
    <stroke
        android:width="1dp"
        android:color="#CFCFCF" />
    <solid android:color="@color/color_selector_thumb" />
</shape>
<!--color/color_selector_thumb-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#333333" android:state_checked="true" />
    <item android:color="#EDEDED" android:state_checked="false" />
</selector>

效果图

Screenrecorder-2020-03-11-10-39-46-784.gif

相关文章

网友评论

      本文标题:Switch使用之Shape更改样式

      本文链接:https://www.haomeiwen.com/subject/qnfgehtx.html