美文网首页
修改Switch的大小及样式

修改Switch的大小及样式

作者: 红鲤鱼与绿鲤鱼与驴与鱼 | 来源:发表于2022-07-07 15:46 被阅读0次

今天在做项目的时候发现Switch的大小不能调,设置了layout_widthlayout_height 并不好使
经过面向百度编程才知道如果想改变它的大小需要设置两个属性android:thumbapp:track 这两个属性。解释一下这两个属性是什么意思

image.png
  • thumb 这个就是设置那个圆圈的
  • track 就是设置轨道的

1、设置 track属性

在drawable下新建 track_switch_normal.xml 轨道未选中时的样式。
注意:轨道的大小在这里设置,(我这里用到了屏幕适配,可以忽略)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置轨道的大小-->
    <size
        android:width="@dimen/dp_64"
        android:height="@dimen/dp_32" />
    <!--填充色-->
    <solid android:color="#C5C8D9" />
    <!--圆角-->
    <corners android:radius="@dimen/dp_20" />
</shape>

新建 track_switch_selected.xml ,轨道选中时的样式,这里只有颜色不一样

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--大小最好和上面一致,要不然选中和未选中的大小不一样-->
    <size
        android:width="@dimen/dp_64"
        android:height="@dimen/dp_32" />
    <solid android:color="#FF6720" />
    <corners android:radius="@dimen/dp_20" />
</shape>

再新建一个选择器,selector_switch_track.xml

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

最后就是应用了,对Switch 的轨道(track)属性设置上选择器就可以了

        <androidx.appcompat.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:track="@drawable/selector_switch_track" />

2、设置thumb属性

步骤其实和上面一样的,只不过是样式不一样
新建 thumb_switch_normal.xml 圆按钮未选中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置圆按钮的大小-->
    <size
        android:width="@dimen/dp_30"
        android:height="@dimen/dp_30" />
    <!--填充色-->
    <solid android:color="@color/rc_white_color" />
    <!--边框的颜色的宽度-->
    <stroke
        android:width="@dimen/dp_2"
        android:color="#C5C8D8" />
    <!--圆角-->
    <corners android:radius="@dimen/dp_20" />
</shape>

新建thumb_switch_press.xml 圆按钮选中,只是颜色不一样

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="@dimen/dp_30"
        android:height="@dimen/dp_30" />
    <solid android:color="@color/rc_white_color" />
    <stroke
        android:width="@dimen/dp_2"
        android:color="#FF6720" />
    <corners android:radius="@dimen/dp_20" />
</shape>

新建 选择器selector_switch_thumb.xml

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

最后设置给Switch

        <androidx.appcompat.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:thumb="@drawable/selector_switch_thumb"
            app:track="@drawable/selector_switch_track" />

上面这些代码是从项目中拷出来并改了的,可能会存在一些颜色或者图标什么的找不到,自己替换一下就行

完!!!!!

相关文章

网友评论

      本文标题:修改Switch的大小及样式

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