开关Switch系列:Switch修改滑块(thumb)和滑道(track)的颜色(一)

  • 看设计稿:
    在这里插入图片描述
    在这里插入图片描述
  • 这尼玛咋弄呀?
  • 先显示文字,参考上一篇:开关switch系列:android Switch显示文字
  • 修改滑块(thumb)的颜色,在drawable文件夹下新建switch_custom_thumb.xml文件,颜色和宽高都是设计图给的,如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#06BF5F" />
    <size
        android:width="27dp"
        android:height="27dp" />
</shape>
  • 修改滑道(track)的颜色,在drawable文件夹下新建switch_custom_track.xml文件,仔细观察设计稿,会发现滑块的高度大于滑道,所以使用到layer-list来实现类似padding的效果,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:top="1dp"
    android:bottom="1dp">

    <shape
        android:shape="rectangle">
        <solid android:color="#00000000"/>
        <size android:height="25dp"/>
        <stroke
            android:width="3dp"
            android:color="#06BF5F" />
        <corners android:radius="15dp" />
    </shape>
</item>
</layer-list>
  • Switch里面引用,如下:
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:showText="true"
        android:switchMinWidth="47dp"
        android:switchTextAppearance="@style/SwitchTextAppearance"
        android:textOff="中"
        android:textOn="EN"
        android:thumb="@drawable/switch_custom_thumb"
        android:track="@drawable/switch_custom_track" />

在这里插入图片描述

和设计稿的效果,还是差一点,主要是宽度比较长,貌似系统好像又比例。下一篇继续研究破解之道。
搞定~

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值