今天介绍的开源项目是否的优秀,又是国人的作品。之前我接触过很多很多的自定义switch,有些动画僵硬,有些不能自定义switch的宽度,有些只能定义宽度不能设置滑块的宽高。但,这个项目提供了各种定制的选项,堪称完美!

项目地址:https://github.com/kyleduo/SwitchButton

作者博客:http://www.kyleduo.com/

一、在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的更多相关文章

  1. HelloGitHub 月刊最受欢迎的开源项目 Top10(2020 年)

    作者:HelloGitHub-卤蛋 2020 年已成往事,2021 年悄然而至. 在已经过完的 2020 年里 HelloGitHub 共发布了 12 期月刊,推荐了 419 个开源项目.​每个月的 ...

  2. Google开源项目风格指南

    Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...

  3. GitHub 优秀的 Android 开源项目(转)

    今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...

  4. GitHub 优秀的 Android 开源项目

    转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...

  5. Android 开源项目分类汇总(转)

    Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...

  6. GitHub 优秀Android 开源项目

    阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11 ...

  7. 【转】GitHub 优秀的 Android 开源项目

    转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...

  8. 【Android 应用开发】GitHub 优秀的 Android 开源项目

    原文地址为http://www.trinea.cn/android/android-open-source-projects-view/,作者Trinea 主要介绍那些不错个性化的View,包括Lis ...

  9. Android常用酷炫控件(开源项目)github地址汇总

    转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...

随机推荐

  1. .NetCore中使用ExceptionLess 添加操作日志

    上一篇文章已经扩展了日志,下面我们在结合下处理操作日志 通常我们想到操作日志 可能想到的参数可能有 模块 方法 参数内容 操作人 操作时间 操作 Ip 下面我们就来结合这些信息添加操作日志 如果要在代 ...

  2. nginx log 错误502 upstream sent too big header while reading response header from upstream

    cookies的值超出了范围我是说 看看了一下日志 错误502 upstream sent too big header while reading response header from upst ...

  3. Java第三阶段学习(十、XML学习)

    一.XML学习 1.模拟Servlet执行 在学习完前端及java与数据库后,将进行WEB编程阶段的学习.在WEB编程中,可以通过浏览器访问WEB服务器上的数据.这时WEB服务器就相当于另一台计算机. ...

  4. pig cookbook学习

    pig cookbook学习 Overview 近期需要用pig做一些统计,由于没有系统学习,问题出现一些问题,且不容易调试,执行效率也不高.所以打算看一些官方文档,在此做些笔记. pig性能提升 指 ...

  5. MVC的博客

    一个基于Asp.net MVC的博客类网站开源了!   背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少 ...

  6. spark sql中将数据保存成parquet,json格式

    val df = sqlContext.load("/opt/modules/spark1.3.1/examples/src/main/resources/people.json" ...

  7. [Python]conda与 virtualenv虚拟环境配置

    参考: Anaconda使用总结 Anacodna之conda与 virtualenv对比使用教程,创建虚拟环境 conda设置Python虚拟环境 python的virtualenv环境与使用 有时 ...

  8. SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.

    SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after ...

  9. strlen()和mb_strlen()

    换行需要用双引号,单引号只会输出字符. strlen()返回字符串所占的字节数,对于utf8编码的中文,一个汉字占三个字节. mb_strlen()返回字符个数,如果不写第二个参数,就会使用内部编码, ...

  10. Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...