Transform Switch to IOS style

Transform the style of Switch to IOS effect

layout

<androidx.appcompat.widget.SwitchCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:checked="false"
        android:thumb="@drawable/switch_button_thumb"
        app:track="@drawable/switch_button_track" />

thumb and track need to be customized.

thumb

switch_button_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">

        <shape android:shape="oval">
            <solid android:color="#ffffff" />
            <size
                android:width="18dp"
                android:height="18dp" />
        </shape>
    </item>
</layer-list>

Use layer-list, so that you can set up, down, left, and right gaps in the item. If you don't set this gap, the effect will not be achieved.

track

switch_button_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <corners android:radius="12dp" />
            <solid android:color="#E61045" />
            <size android:width="36dp" android:height="20dp" />
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <corners android:radius="12dp" />
            <solid android:color="#eeeeee" />
            <size android:width="36dp" android:height="20dp" />
        </shape>
    </item>
</selector>

Set the style of the selected and unselected states.

Guess you like

Origin blog.csdn.net/ganduwei/article/details/120107812