自定义switch控件

需要在drawable目录下新建四个xml文件

switch_thumb_on.xml

  1. <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
    
        <solid
            android:color="@android:color/holo_green_dark">
        </solid>
        <size
            android:width="30dp"
            android:height="30dp">
        </size>
        <corners
            android:radius="32dp">
        </corners>
    
    
    </shape>

    switch_thumb_off.xml 

  2. <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="#7c7c7c">
        </solid>
    
        <size
            android:width="30dp"
            android:height="30dp">
        </size>
        <corners
            android:radius="32dp">
        </corners>
    
    </shape>

    switch_track_off.xml

  3. <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid
            android:color="@android:color/darker_gray">
        </solid>
        <stroke android:color="#00000000"
            android:width="20dp" />
    
        <corners
            android:radius="20dp">
        </corners>
    
    </shape>
    

    switch_teack_on.xml

  4. <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid
            android:color="@android:color/holo_green_light">
        </solid>
        <stroke android:color="#00000000"
            android:width="20dp"/>
    
        <corners
            android:radius="20dp">
        </corners>
    
    </shape>
    

    前两个为可移动滑块的开关xml文件,后面两个为滑块轨迹的xml文件,接下来再新建一个xml文件switch_track.xml

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

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


</selector>

这个xml文件规定了滑块不同状态引用对应的xml文件,此时有两种做法:

方法1. 在value-->styles.xml中添加

style name="My.TextAppearance" parent="@style/TextAppearance.AppCompat.Widget.Switch">
 
        <item name="android:textSize">20dp</item>
 
        <item name="android:textColor">#000</item>
 
    </style>

然后在你布局的xml文件中添加:

<Switch
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="348dp"
        android:layout_weight="1"
        android:checked="false"
        android:switchTextAppearance="@style/My.TextAppearance"
        tools:checked="true" />

运行模拟器,就可以看到自定义switch。

方法2.再新建一个xml文件,switch_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <item android:state_checked="true"
    android:drawable="@drawable/switch_thumb_on"/>
    <item android:state_checked="false"
        android:drawable="@drawable/switch_thumb_off"/>

</selector>

再去布局文件引用switch控件即可

<Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="248dp"
        android:switchMinWidth="70dp"
        android:checked="false"
        android:showText="true"
        android:textOn="开"
        android:textOff="关"
        android:thumb="@drawable/switch_thumb"
        android:track="@drawable/switch_track"
        tools:checked="true" />

这里前面的移动轨迹xml中

<stroke android:color="#00000000"
    android:width="20dp" />

代表用一个透明边框去约束里面的轨迹从而实现,轨迹为一条细线,这里感谢我的同事对我的指导@jerome

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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状态改变时,背景样式也会随之改变。你可以根据自己的需求来修改样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值