Android 自定义Switch 样式

自定义Switch样式

自定义Switch首先要熟悉两个属性,thumb与trace
-. thumb 代表按钮的意思,就是switch左右两边那个
-. trace 代表轨迹的意思,就是thumb在来回滑动过程中变化的轨迹

. 自定义thumb
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<size android:width="30dp"
android:height="30dp"/>
<corners android:radius="180dp"/>
</shape>

上述代码就是创建了一个宽高相同的thumb,当然如果你有其他需求也可以改成其他大小颜色的

自定义trace关闭状态

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#dbdbdb"/>
    <corners android:radius="20dp"/>
</shape>

自定义trace打开状态

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fe8a01"/>
    <corners android:radius="20dp"/>
</shape>

自定义trace选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:drawable="@drawable/shape_switch_track_open"   android:state_checked="true"/>
    <item android:drawable="@drawable/shape_switch_track_close"
        android:state_checked="false"/>
    <item android:drawable="@drawable/shape_switch_track_close"
        />
</selector>

最终的应用

<Switch
        android:id="@+id/st_switch"
        android:layout_width="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:thumb="@drawable/shape_switch_thumb"
        android:track="@drawable/shape_switch_track_selector"
        android:layout_height="wrap_content" />

最后

switch的大小是跟thumb的大小有关

这里写图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android自定义Switch控件可以通过自定义drawable来实现。以下是一个简单的例子: 1. 创建一个drawable资源文件,例如 switch_bg.xml,用于定义Switch的背景样式: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/switch_on_bg" /> <item android:state_checked="false" android:drawable="@drawable/switch_off_bg" /> </selector> ``` 2. 创建两个drawable资源文件,例如 switch_on_bg.xml 和 switch_off_bg.xml,分别用于定义Switch开和关状态下的样式switch_on_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#00FF00" /> <corners android:radius="20dp" /> </shape> ``` switch_off_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF0000" /> <corners android:radius="20dp" /> </shape> ``` 3. 在布局文件中使用自定义Switch控件: ```xml <Switch android:id="@+id/customSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/custom_switch_thumb" android:track="@drawable/switch_bg" /> ``` 其中,android:thumb属性定义了Switch的拇指(即开关按钮)的样式。我们可以创建一个自定义drawable来实现: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFFFF" /> <size android:width="20dp" android:height="20dp" /> </shape> ``` 这样就完成了自定义Switch控件的样式。当Switch状态改变时,背景样式也会随之改变。你可以根据自己的需求来修改样式
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值