Android 自定义Switch开关按钮的样式实例详解

所属分类: 软件编程 / Android 阅读数: 42
收藏 0 赞 0 分享

封面

GitHub传送门

1.写在前面

本文主要讲的是在Android原生Switch控件的基础上进行样式自定义,内容很简单,但是在实现的过程中还是遇到了一些问题,在此记录下来,希望对大家能够有所帮助,看下效果图:

自定义样式

2.自定义样式

2.1 原生样式

首先看下原生的效果(Android 7.1):

原生效果

布局文件如下:

<Switch
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

2.2 自定义样式

设计给的效果图大多数都不会使用原生效果,所以我们需要对样式进行自定义,比如下面这种效果:

自定义效果

定义Switch的开关按钮状态:

开启状态:switch_custom_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="#94C5FF" />
 <size
  android:width="20dp"
  android:height="20dp" />
</shape>

关闭状态:switch_custom_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="#AAA" />
 <size
  android:width="20dp"
  android:height="20dp" />
</shape>

定义一个selector:switch_custom_thumb_selector.xml

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

到此Switch的开关按钮状态就定义好了,接下来定义一下Switch滑动轨道的状态:

开启状态:switch_custom_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="#B6D6FE" />
 <stroke
  android:width="5dp"
  android:color="#00000000" />
 <corners android:radius="20dp" />
</shape>

关闭状态:switch_custom_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="5dp"
  android:color="#00000000" />
 <corners android:radius="20dp" />
</shape>

定义一个selector:switch_custom_track_selector.xml

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

Switch自定义样式,默认情况下开关按钮和滑动轨道的高度是一样的,并且在xml文件中对轨道的宽高设置是无效的,如果想要修改轨道的高度可以这样做:

轨道高度低于开关按钮高度(效果中的第一个效果):轨道增加一个透明的边框

轨道高度高于开关按钮高度(效果中的第二个效果):开关按钮增加一个透明的边框

轨道的宽度会随着开关按钮的宽度自动变化,如果想要修改轨道的宽度,修改开关按钮的宽度就可以了。

设置自定义样式

thumb是开关按钮的属性,track是滑动轨道的属性,只需要把上面的两个selector文件设置进去就大功告成了。

<Switch
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:thumb="@drawable/switch_custom_thumb_selector"
 android:track="@drawable/switch_custom_track_selector" />

3.更多属性

如果想要在开关按钮上显示文字怎么办,textOn和textOff属性可以分别设置开启和关闭的文字,别忘了将showText属性设置为true,这样才能显示出来:

<Switch
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:showText="true"
 android:switchTextAppearance="@style/SwitchTheme"
 android:textOff="OFF"
 android:textOn="ON"
 android:thumb="@drawable/switch_rectangle_thumb_selector"
 android:track="@drawable/switch_rectangle_track" />

显示文字还不够,还需要修改文字的颜色:

在res文件夹下建一个color文件夹,定义一个文本颜色状态的selector:switch_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="#FFF" android:state_checked="false" />
 <item android:color="#000" android:state_checked="true" />
</selector>

然后在style文件中定义一个样式:

<style name="SwitchTheme" parent="@android:style/TextAppearance.Small">
 <item name="android:textColor">@color/switch_text_selector</item>
</style>

最后在Switch中设置一下就可以了:

android:switchTextAppearance="@style/SwitchTheme"

4.写在最后

本文只讲了效果图中第一种样式的实现方法,更多样式可以在GitHub上进行下载查看,如有疑问,可以给我留言。

GitHub传送门

以上所述是小编给大家介绍的Android 自定义Switch开关按钮的样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多