自定义switchCompat背景样式+动画

自定义switchCompat样式

翻了一遍switchCompat详解,要想自己改掉原生样式成上图样板,改掉原生track 和 thumb即可, 话不多说上demo

新建thumb_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/thumb_on" android:state_checked="true">

<item android:drawable="@drawable/thumb_off" android:state_checked="false">

</selector>


thumb_on 和 thumb_off 样式:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"  >

    <shape android:shape="rectangle">

        <size android:width="28dp" android:height="28dp" />

        <gradient android:startColor="#06b4ea" android:endColor="#009ee8" android:angle="-90" />

        <corners android:radius="20dp" />

<item android:gravity="center"  >

    <shape android:shape="oval"  >

        <solid android:color="#FFFFFF"/>

        <size android:width="16dp" android:height="16dp" />

</layer-list>


<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <shape android:shape="rectangle">

            <size android:width="28dp" android:height="28dp" />

            <corners android:radius="20dp" />

        android:gravity="center" >

        <shape android:shape="oval">

            <solid android:color="#19191919"  />

            <size android:height="16dp" android:width="16dp" />

</layer-list>


track_selector , track_on, track_off :


<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <item android:state_checked="true"  android:drawable="@drawable/track_on" />

    <item android:drawable="@drawable/track_normal" />

</selector>


<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <size android:width="56dp" android:height="28dp" />

    <gradient android:startColor="#06b4ea" android:endColor="#009ee8" android:angle="-90" />

    <corners android:radius="20dp" />

</shape>


<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <size android:width="56dp" android:height="28dp" />

    <solid android:color="#0c000000" />

    <stroke android:color="#19191919" android:width="1dp" />

    <corners android:radius="20dp" />

</shape>


xml 中引用如下:

<androidx.appcompat.widget.SwitchCompat

    android:id="@+id/switch_compat"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    app:switchMinWidth="56dp"

    android:background="@null"

    app:track="@drawable/track_selector"

    android:thumb="@drawable/thumb_selector"

    app:theme="@style/switchStyle"/>


有些机型可能还是会展示原生样式, 则需自己把原生样式统统去掉,styles.xml新增:

<style name="switchStyle" parent="Theme.AppCompat.Light">

    <item name="colorControlActivated">#00000000

    <item name="colorSwitchThumbNormal">#00000000

    <item name="android:colorForeground">#00000000

</style>

xml中用app:theme调用即可。

OK啦~~~~~·

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 进口蔓越莓干需要哪些资料 作为一家国际物流有限公司,青岛金邦一直秉承着”推己及人、视野开拓、热情务实”的服务精神,...
    4d8be78c42e8阅读 129评论 0 0
  • 10min,逻辑很简单,使用两个栈,就不用考虑下一层有多少个节点了 7min,是前序遍历,但是通过函数参数把层数记...
    mztkenan阅读 138评论 0 0
  • 宋甜 【日精进打卡第4天】 【知~学习】 《道德经阐微》六十五、六十六章 《京瓷哲学》501-510页 【行~实践...
    和铃央央_a338阅读 187评论 0 0
  • 六年级15班杨景涛 清明时节雨,我漫步乡野,任飘洒的雨丝轻抚我的脸颊。 四月的田野是一片葱绿,郁郁葱葱的禾...
    雨后阳光_ad99阅读 145评论 0 0
  • 今个儿一大早去省妇幼谈个买卖,怎么样,够牛吧,其实也就几包卫生巾的事。 下了公交往医院走,一抬头就看见电线杆...
    锦瑟华年与谁度阅读 124评论 0 0