Android

Android Swtich 스위치 ( 커스텀 )

Machine_웅 2024. 1. 11. 14:53
728x90
반응형


<com.google.android.material.switchmaterial.SwitchMaterial
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:useMaterialThemeColors="false"
    app:switchMinWidth="40dp"
    app:track="@drawable/switch_track_selector"
    android:thumb="@drawable/switch_thumb_selector"
    />

 

 

switch_thumb_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <!-- 스위치의 배경-->
    <item
        android:drawable="@drawable/switch_thumb_off"
        android:state_checked="false"/>

    <item
        android:drawable="@drawable/switch_thumb_on"
        android:state_checked="true"/>

</selector>

 

switch_thumb_on

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:dither="true"
    android:useLevel="false"
    >

    <solid android:color="#ffffff" />

    <stroke
        android:width="2dp"
        android:color="#00FF2B"/>

    <size android:width="22dp"
        android:height="22dp"/>

</shape>

 

 

switch_thumb_off

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:dither="true"
    android:useLevel="false"
    >

    <solid android:color="#ffffff" />

    <stroke
        android:width="2dp"
        android:color="#dfdfdf"/>

    <size android:width="22dp"
        android:height="22dp"/>

</shape>

 

 

switch_track_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 스위치의 배경-->
    <item
        android:drawable="@drawable/switch_track_off"
        android:state_checked="false"/>

    <item
        android:drawable="@drawable/switch_track_on"
        android:state_checked="true"/>

</selector>

 

 

switch_track_off

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:dither="true"
    android:useLevel="false"
    >

    <solid android:color="#ffffff" />

    <stroke
        android:width="2dp"
        android:color="#dfdfdf"/>

    <size android:width="22dp"
        android:height="22dp"/>

</shape>

 

switch_track_on

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="15dp" />

    <!-- 트랙의 width / height
    사실 width는 위젯 속성 중 'app:switchMinWidth'에 의해 설정되므로 크게 상관은 없습니다. -->

    <size
        android:width="40dp"
        android:height="20dp" />

    <solid
        android:color="#00FF2B" />


</shape>
728x90
반응형