用开源项目SwitchButton实现各种风格的switch

今天介绍的开源项目是否的优秀,又是国人的作品。之前我接触过很多很多的自定义switch,有些动画僵硬,有些不能自定义switch的宽度,有些只能定义宽度不能设置滑块的宽高。但,这个项目提供了各种定制的选项,堪称完美!
项目地址:https://github.com/kyleduo/SwitchButton
一、在xml中使用控件
下载并导入lib后,我们在xml中放上这个控件就好了
<com.kyleduo.switchbutton.SwitchButton
android:id="@+id/sb_default"
style="@style/SwitchButtonStyle"
android:layout_width="200dp"
android:layout_height="100dp"
app:thumb_width="90dp"
app:thumb_height="90dp"/>
别忘记加命名空间:xmlns:app="http://schemas.android.com/apk/res-auto"
值得称赞的是,这个控件在eclipse预览时提供了完美的实时预览:

二、xml中的各种属性
In xml layout file, you can configure the face of switch button using these attrs.
- onDrawable: drawable of background for status ON
- offDrawable: drawable of background for status OFF
- thumbDrawable: drawable of thumb
- thumb_margin: set inner margin between thumb and edges
- thumb_marginLeft/Top/Bottom/Right: set margin for specific edge
- thumb_width: set the width of thumb, probably used for gradient drawable
- thumb_height: set the height of thumb
- onColor: set the color of status ON, usd for flat version, the priority is below of onDrawable
- offColor: like the onColor
- thumbColor: like the onColor
- thumbPressedColor: like the thumbColor, but for pressed status
- animationVelocity: distance of animation per frame
- radius: used for color version, radius of corner of background and thumb.
- measureFactor: factor limit the minimum width equals almost (the height of thumb * measureFactor)
如果你想要定义style的话,可以在style文件中写
<style name="MaterialDesignStyle" parent="MD">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">16dp</item>
</style>
总之是各种方便。我们还可以参照demo,来自定义滑块和背景,还可以定义switch的滑动条。具体请大家看demo吧。
三、Java中的操作
3.1 监听器
// 监听器来监听事件
sbDefault.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(StyleActivity.this, "Default style button, new state: " + (isChecked ? "on" : "off"), Toast.LENGTH_SHORT).show();
}
});
3.2 设置样式
Configuration conf = Configuration.getDefault(getResources().getDisplayMetrics().density);
conf.setThumbMargin(4); // 设定小滑块的边界
conf.setVelocity(8); // 设置点击滑块,滑动开启/关闭的速率
conf.setThumbWidthAndHeight(40, 14); // 小滑块的宽高
conf.setRadius(10); //设置边缘弧度数
conf.setMeasureFactor(2f); // 限制最小的宽度
sbInCode.setConfiguration(conf); // 给switch应用上述设置
3.3 设置状态
sb.setEnabled(isChecked);
有动画的状态切换,设置后switch改变状态时会伴随着滑块动画
mCheckedSb.slideToChecked(!mCheckedSb.isChecked()); // 滑动切换开关效果
3.4 翻转当前状态
用toggle()的话,在切换时会出现滑动的动画
Sb.toggle(); // 将switch切换为当前状态的相反状态。也就是如果当前是开启,那么变成关闭。如果是关闭,就变为开启。
用toggle(false),在切换时就不显示动画
Sb.toggle(false);
源码下载:http://download.csdn.net/detail/shark0017/8372607
用开源项目SwitchButton实现各种风格的switch的更多相关文章
- HelloGitHub 月刊最受欢迎的开源项目 Top10(2020 年)
作者:HelloGitHub-卤蛋 2020 年已成往事,2021 年悄然而至. 在已经过完的 2020 年里 HelloGitHub 共发布了 12 期月刊,推荐了 419 个开源项目.每个月的 ...
- Google开源项目风格指南
Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...
- GitHub 优秀的 Android 开源项目(转)
今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...
- GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
- Android 开源项目分类汇总(转)
Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...
- GitHub 优秀Android 开源项目
阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11 ...
- 【转】GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
- 【Android 应用开发】GitHub 优秀的 Android 开源项目
原文地址为http://www.trinea.cn/android/android-open-source-projects-view/,作者Trinea 主要介绍那些不错个性化的View,包括Lis ...
- Android常用酷炫控件(开源项目)github地址汇总
转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...
随机推荐
- C语言——第零次作业
A:你对网络专业或计算机专业了解是怎样? 据我所知计算机专业未来前景不错,不仅就业路子广,毕业工资高,而且在全世界都很吃香.但是,计算机专业是一个非常难学的专业,尽管人们追趋逐耆也没有填补计算机高端人 ...
- Git(三)Git的远程仓库
一. 添加远程库 现在我们已经在本地创建了一个Git仓库,又想让其他人来协作开发,此时就可以把本地仓库同步到远程仓库,同时还增加了本地仓库的一个备份.常用的远程仓库就是github:https://g ...
- 【LOJ】#2479. 「九省联考 2018」制胡窜
题解 老了,国赛之前敲一个后缀树上LCT和线段树都休闲的很 现在后缀树上线段树合并差点把我写死 主要思路就是后缀树+线段树合并+容斥,我相信熟练的OIer看到这已经会了 但就是不想写 但是由于我过于老 ...
- 【LOJ】#2109. 「JLOI2015」骗我呢
题解 我深思熟虑许久才算是明白个大概的计数问题吧 先是转化成一个矩形,列一条直线y = x,y = x - (m + 1)我们从(0,0)走到(n + m + 1,m + 1)就是答案 因为我们起始相 ...
- P1164 小A点菜 洛谷
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...
- IO知识点整理(文件File类的使用)
一: 1.API 2.构造函数的程序 注意这集中构造函数的特点. 同时,字段separator的使用. import java.io.File; public class Test101 { publ ...
- redis 持久化的两种方式
一:快照模式 或许在用Redis之初的时候,就听说过redis有两种持久化模式,第一种是SNAPSHOTTING模式,还是一种是AOF模式,而且在实战场景下用的最多的 莫过于SNAPSHOTTING模 ...
- Spring框架学习01——使用IDEA开发Spring程序
1.创建项目 点击“Create New Project”,新建项目 选择Maven项目 项目配置 使用本地安装的Maven 一直点击Next,最后点击完成当控制台中出现“BUILD SUCCESS” ...
- BZOJ4556 HEOI2016字符串
没错,又是这题,使用后缀自动机,反向建树,主席树维护right集合. By:大奕哥 #include<bits/stdc++.h> using namespace std; ; ]; ch ...
- UVALive 6916 Punching Robot dp
Punching Robot 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...