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

今天介绍的开源项目是否的优秀,又是国人的作品。之前我接触过很多很多的自定义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

时间: 2024-08-02 17:05:24

用开源项目SwitchButton实现各种风格的switch的相关文章

【代码导读】Github 开源项目——wysihtml5 富编辑器(Bootstrap 风格)【二】

如果您意外进入该页面,或许从下述链接开始更容易理解: [代码导读]Github 开源项目——wysihtml5 富编辑器(Bootstrap 风格)[一] 1. 整体结构 在 wysihtml5 中,所有对象受 wysihtml5 命令空间保护,从其初始化定义看,整个代码架构一目了然: https://github.com/xing/wysihtml5/blob/master/src/wysihtml5.js 1 var wysihtml5 = { 2 version: "0.4.0pre&qu

android 5.0 Material Design酷炫风格的开源项目集合

一.前言 Android 5.0上线有一个段时间了,估计小伙伴们都看到了Android5.0界面上相比前面几个版本有了很大的突破,给人一种非常酷炫,平滑的跳转,生动的界面切换,全新的感觉,Android用户终于可以感叹,Google升级了这么多个版本终于像样的搞了一回界面.开发者们看到这些优美的动画也要大户过瘾啊.github上面的高人们都已经饥渴难耐了出了写出了各种好用,炫丽的特效,我们今天就来看一下都有哪些可以用比较爽的开源项目. Material Menu 项目地址:https://git

Google 开源项目风格指南

Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Python会将 圆括号, 中括号和花括号中的行隐式的连接起来 , 你可以利用这个特点. 如果需要, 你可以在表达式外围增加一对额外的圆括号. Yes: foo_bar(self, width, height, color='black', design=None, x='foo', emphasis=

优秀的 Android 开源项目

摘要  转载http://www.trinea.cn/android/android-open-source-projects-view/,方便大家找到自己合适的资料 目录[-] 一.ListView 二.ActionBar 三.Menu 四.ViewPager .Gallery 五.GridView 六.ImageView 七.ProgressBar 八.其他 GitHub上优秀Android开源项目 3. Android开发神器 1.Xabber客户端 2.oschina客户端 3.手机安全

GitHub 优秀的 Android 开源项目

转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包含ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar及其它如Dialog.Toast.EditText.TableView.Activity Animation等等. 一.ListView android-pulltorefresh 一个强大的拉动

安卓开源项目周报0104

由OpenDigg 出品的安卓开源项目周报第四期来啦.我们的安卓开源周报集合了OpenDigg一周来新收录的优质的安卓开发方面的开源项目,方便安卓开发人员便捷的找到自己需要的项目工具等. PanoramaImageView 可以随设备旋转自动滚动的imageView Android-ExpandIcon Google风格上下箭头的简单自定义实现 material-about-library 为app轻松创建关于页面 android-snowfall 安卓Snowfall View的全自定义实现

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

转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar及其他如Dialog.Toast.EditText.TableView.Activity Animation等等. 一.ListView android-pulltorefresh 一个强大的拉动

GitHub 优秀Android 开源项目

阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11.简洁天气 12.高仿开心网 13.百度推聊 14.餐厅订餐 15.eoe客户端 16.饭否网客户端 17.imiPhoneWall 18.k9mail 19.MSD音乐 20.高仿爱奇艺 21.企信通 22.高仿生日管家 23.PM25 24.高仿陌陌 25.股民邦 26.CB资讯阅读 27.幽秘

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

转载一个很牛逼的控件收集贴... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style 等等. 一.ListView android-pulltorefresh一个强大的拉动