Android Swicth按钮样式自定义

<Switch
  android:id="@+id/switch_btn"
  android:layout_width="wrap_content"
  android:layout_height="24dp"
  android:layout_marginRight="10dip"
  android:checked="true"
  android:splitTrack="false"
  android:switchMinWidth="50dp"
  android:thumb="@drawable/ko_switch_thumb_white"
  android:track="@drawable/ko_switch_track_primary"
  android:typeface="serif" />


ko_switch_thumb_white和ko_switch_track_primary文件定义Switch的样式。

ko_switch_thumb_white.xml

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


ko_switch_track_primary.xml

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


ko_switch_thumb_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/ko_white" />
    <size
        android:width="24dp"
        android:height="24dp" />
    <stroke
        android:width="5dp"
        android:color="#00000000" />
</shape>


ko_switch_thumb_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/ko_white" />
    <size
        android:width="24dp"
        android:height="24dp" />
    <stroke
        android:width="5dp"
        android:color="#00000000" />
</shape>


ko_switch_track_on.xml

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


ko_switch_track_off.xml

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




本站内容来源于作者发布和网络转载,如有版权相关问题请及时与我们取得联系,我们将立即删除。

 关于作者
 热门教程
通过Git下载和提交代码的命令记录
安装好git后: 1、配置邮箱 git config --global user.name ymbok git con
2023-08-15
剑道仙尊
59
通过ADB在Android系统中快捷截屏和录屏的方法
连上ADB线后: 截图 创建一个BAT文件,用于截图,直接双击运行即可将图片保存到D:\screenshot目录,提前
2023-08-15
剑道仙尊
105
Android12 源码下载与编译
下载Android12 源码 sudo apt-get update 安装curl sudo apt install
2023-04-25
剑道仙尊
125
android生成签名文件jks并获取SHA1
打开Android Studio终端,输入: keytool -genkey -alias app -keyalg R
2022-07-13
剑道仙尊
93
Flutter开发APP更改状态栏文字颜色
void main(){ runApp(MyApp()); /// 状态栏文字黑色 SystemChrome
2022-06-09
剑道仙尊
132
Android Swicth按钮样式自定义
&lt;Switch android:id="@+id/switch_btn" android:layout_wi
2022-06-07
剑道仙尊
133
Pagging3写起来太麻烦,简单封装及其简单
Pagging3分页写起来很麻烦,这里分享一下我的简化开发的方法,思路就是把获取数据的函数分离出来 首先定义一个基础的
2022-06-07
剑道仙尊
187
在项目中使用Hilt Retrofit使用总结
直接开始,首先我们看看怎么使用Hilt编写 Retrofit 接口请求类 用@Provides注解定义可注入的实例的提
2022-06-07
剑道仙尊
197
Android Jetpack Paging 3 下拉刷新和加载更多代码示例
使用Paging3实现列表的下拉刷新和加载更多 首先定义列表布局文件 &lt;androidx.swiperefres
2022-06-07
剑道仙尊
434
StatefulBuilder实现Dialog的刷新
在Flutter中使用Dialog时,因为 showDialog返回的context与当前页面的 context不是同
2022-06-07
剑道仙尊
136