switch 使用

通常修改默认的switch样式即可,主要用到两个属性
thumb
track
先看下效果图


sw.png
先创建selector选择器来区别开关不同状态下显示样式
<!-- 底层下滑条的样式选择器,不同状态下,底下下滑条的颜色 switch_track.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_green_track" android:state_checked="true"/>
    <item android:drawable="@drawable/switch_gray_track"/>
</selector>

<!-- switch_green_track.xml -->  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
    <!-- 高度40 -->  
    <size android:height="20dp"/>  
    <!-- 圆角弧度 20 -->  
    <corners android:radius="4dp"/>  
    <!-- 变化率 -->  
    <gradient  
        android:endColor="#d8d6d9"  
        android:startColor="#d8d6d9" />  
</shape>  

<!-- switch_gray_track.xml-->  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" >  
    <!-- 高度30   此处设置宽度无效-->  
    <size android:height="20dp"/>  
    <!-- 圆角弧度 15 -->  
    <corners android:radius="4dp"/>  
    <!-- 变化率  定义从左到右的颜色不变 -->  
    <gradient  
        android:endColor="#949393"  
        android:startColor="#949393" />  
</shape>  


<!-- 按钮的选择器,不同状态下的时候,按钮不同的颜色 switch_thumb.xml -->  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >    
<item android:state_checked="true"  android:drawable="@drawable/switch_green_thumb" />  
<item                               android:drawable="@drawable/switch_gray_thumb" />  
</selector>  

<!-- switch_green_thumb.xml -->  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" >  
    <!-- 高度40 -->  
    <size android:height="20dp" android:width="25dp"/>  
    <!-- 圆角弧度 20 -->  
    <corners android:radius="4dp"/>  
    <!-- 变化率 -->  
    <gradient  
        android:endColor="#0acc0a"  
        android:startColor="#0acc0a" />  
    <stroke android:width="1dp"  
        android:color="#0acc0a"/>  
</shape>  

<!-- switch_gray_thumb.xml -->  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" >  
    <size android:height="20dp" android:width="25dp"/>  
    <!-- 圆角弧度  -->  
    <corners android:radius="4dp"/>  
    <!-- 变化率 -->  
    <gradient  
        android:endColor="#e1e5e1"  
        android:startColor="#e1e5e1" />  
    <stroke android:width="1dp"  
        android:color="#e1e5e1"/>  
</shape>  

最后在布局文件中加入开个按钮。
<Switch
            android:id="@+id/setting_camera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:switchMinWidth="25dp"
            android:textOff=" "
            android:textOn=" "
            android:thumb="@drawable/switch_thumb"
            android:track="@drawable/switch_track" />
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,104评论 25 708
  • 如果你爱它 那你就别采 不是生命的惊奇 不是自然的馈赠 它没那么高雅 它就如她 需要爱 别采她 别带回家 她会枯萎...
    过非往路阅读 162评论 0 0
  • 晨读连接点击阅读☞《剽悍晨读:如何提升自信,勇敢面对生活中的挑战》 参考书目:《自信力:成为最好的自己》[英] 罗...
    研悟阅读 908评论 0 3
  • 所在文集 青春 注:人名,地名纯属虚构 我叫白晓花。 我的人生,宛若一个笑话。 故事都是从头说起,而我喜欢一笔带过...
    赏清欢阅读 344评论 0 1