美文网首页
Switch自定义样式

Switch自定义样式

作者: 颜崔 | 来源:发表于2017-03-11 11:15 被阅读0次
    效果
    ares.gif
    布局使用
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:switchMinWidth="55dp"
        android:textOff=""
        android:textOn=""
        android:thumb="@drawable/switch_selector"
        android:track="@drawable/switch_track" />
    
    switch_selector.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true" android:drawable="@drawable/thumb_on"/>
        <item android:state_checked="true" android:drawable="@drawable/thumb_on"/>
        <item android:drawable="@drawable/thumb_off"/>
    
    </selector>
    
    thumb_on.xml
    <?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="20dp"/>
    
        <stroke android:width="1dp"
            android:color="#00ffffff"/>
    
        <corners android:bottomLeftRadius="20dp"
            android:bottomRightRadius="20dp"
            android:topLeftRadius="20dp"
            android:topRightRadius="20dp"/>
    
        <solid android:color="#00bca1" />
    </shape>
    
    thumb_off.xml
    <?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="20dp"/>
    
        <stroke android:width="6dp"
            android:color="#00ffffff"/>
    
        <corners android:bottomLeftRadius="20dp"
            android:bottomRightRadius="20dp"
            android:topLeftRadius="20dp"
            android:topRightRadius="20dp"/>
    
        <solid android:color="#c5c5c5" />
    </shape>
    
    switch_track.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_focused="true" android:drawable="@drawable/track_on"/>
        <item android:state_checked="true" android:drawable="@drawable/track_on"/>
        <item android:drawable="@drawable/track_off"/>
    
    </selector>
    
    track_on.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <size android:height="21dp"
            android:width="55dp"/>
    
        <corners android:radius="12dp" />
    
        <stroke android:width="2px"
            android:color="#00bca1"/>
    
        <solid android:color="#00ffffff" />
    
    </shape>
    
    track_off.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <size android:width="55dp"
            android:height="21dp"/>
    
        <corners android:radius="12dp" />
    
        <stroke android:width="2px"
            android:color="#c5c5c5"/>
    
        <solid android:color="#00ffffff" />
    </shape>
    

    相关文章

      网友评论

          本文标题:Switch自定义样式

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