上周使用Android的switch模仿iOS的左右滑动开关控件,代码如下:

aty_switch.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.suhongwei897.learnandroid.aty.AtySwitch"><Switchandroid:id="@+id/mySwitch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:switchMinWidth="30dp"android:textOff=""android:textOn=""android:thumb="@drawable/thumb"android:thumbTextPadding="16dp"android:track="@drawable/track" /></RelativeLayout>

track.xml:

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

track_on.xml:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#EB520E" /><size android:width="40dp"android:height="30dp"/><cornersandroid:radius="15dp"/>
</shape>

track_off.xml:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#bdbdbd" /><size android:width="40dp"android:height="30dp"/><cornersandroid:radius="15dp"/>
</shape>

thumb.xml:

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

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="@android:color/white" /><strokeandroid:width="1dp"android:color="#EB520E" /><sizeandroid:width="30dp"android:height="30dp" />
</shape>

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="@android:color/white" /><strokeandroid:width="1dp"android:color="#bdbdbd" /><sizeandroid:width="30dp"android:height="30dp" />
</shape>

AtySwitch.class:

mySwitch = (Switch) findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {mySwitch.setSelected(isChecked);}
});

以下两个值都是需要设置的:

android:switchMinWidth="30dp"

android:thumbTextPadding="16dp"

Android仿iOS左右滑动开关控件(Android4.0以上适用)相关推荐

  1. android 仿ios三级联动,仿iOS的PickerView控件,有时间选择和选项选择并支持一二三级联动效果...

    Android-PickerView 注意事项.详请使用方式.更新日志等,请查看 Wiki文档 Wiki文档,Wiki文档,Wiki文档 !~ 重要的事情说三遍 对于使用上有任何疑问或优化建议等,欢迎 ...

  2. android 仿照ios 图片选择,GitHub - wildma/PictureSelector: Android 图片选择器(仿 IOS 图片选择控件)...

    PictureSelector Android 图片选择器(仿 IOS 图片选择控件) 效果图 功能特点 支持通过拍照获取图片 支持通过相册获取图片 支持图片是否裁剪两种场景 支持仿 IOS 底部弹出 ...

  3. Android 实现 IOS相机滑动控件

     IOS相比于Android,动画效果是一方面优势,IOS相机切换时滑动的动画很不错,看着是有一个3D的效果,而且变化感觉很自然.Android也可以通过Graphics下面的Camera可以实现3D ...

  4. Android仿同花顺自选股列表控件

    介绍 RecyclerView的开发中,我们通常会遇到一行显示不下内容的情况,产品会要求我们的item是可以滚动的,并且头部是固定的.特别在股票行情类相关的app上,这样的场景是非常多的,所以封装了如 ...

  5. Android仿超级课程表开源控件

    # TimetableView(v1.x) TimetableView已经升级到v2.0.2了,新增了很多实用的功能 本文中的功能介绍.控件用法均针对v1.x,它并不是最新的,你可以在 Change ...

  6. Android仿苹果siri浮动控件

    实现原理是用WindowManager添加视图,生命周期可以不跟随某一页面或某一应用,可以全屏浮动(状态栏除外).只有一个主类FloatView.在应用中new一下就可以显示了. 需要添加权限:< ...

  7. android酷狗下拉,Android 仿酷狗滑动控件实现 流畅运行

    最近又浏览了一遍github上的开源项目,有感于大神们的强大,所以想搞搞像kugou那样左右滑动的侧滑菜单控件. 打开移动设备的kugou软件,看了下整体效果,就知道了,我们要实现一个左侧主页,右侧菜 ...

  8. android仿ios开关按钮

    转载:http://blog.csdn.NET/bfbx5173/article/details/45191147 自学Android差不多有一年了,从最初的小白菜鸟,摸爬滚打,看大神们的博客,and ...

  9. android开发仿原生联系人,android 仿IOS联系人列表

    [实例简介] 这是一款仿ios联系人列表控件,支持字母行置顶(有阴影和没阴影置顶).支持右边字母视图点击和滑动到置顶的字母行.搜索栏支持中英文搜索 [实例截图] [核心代码] PhoneDemo └─ ...

  10. android 仿ios毛玻璃,类 iOS 毛玻璃效果控件 BlurView

    软件介绍 BlurView 是Android下类似 iOS 毛玻璃效果控件. 使用: android:id="@+id/blurView" android:layout_width ...

最新文章

  1. linux 内核 时间片,能讲一下在Linux系统中时间片是怎么分配的还有优先级的具体算法是...
  2. 4.9 内容代价函数-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  3. 操作系统(4)-进程间通信
  4. 智慧消防、消防管理、事件管理、维保巡检、应急管理、培训管理、值班管理、考试管理、设备列表、机构管理、应急预案、axure原型、rp原型
  5. spring cloud 微服务的版本介绍与内部组件详解
  6. JavaScript常见鼠标事件与用法分析
  7. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
  8. 模型预测控制的缺点_模型预测控制MPC的通俗解释
  9. svn忽略指定文件或目录
  10. nssa和stub_OSPF的特殊区域--stub及totally-stub及nssa及totally-nssa详解
  11. ue4 计算向量夹角
  12. sol日历只能在android,日历本应如此优美 Sol日历For Android体验
  13. Unity 灯光设置——灯光类型
  14. 华为OD机试 - 荒地(Java JS Python)
  15. android 三大框架是什么,聊聊Android中的三大框架
  16. BACnet IP通讯方式组网步骤
  17. Weblogic常见报错以及解决方法[转载]
  18. 开发 HTML5 小游戏
  19. 人工智能之状态空间表示法的简单应用之翻转三枚钱币
  20. 杂散干扰解决办法_6种常见杂散的成因分析及解决办法

热门文章

  1. diybox路由器设置教程_无线路由器设置图解,最全面的图文教程
  2. python语言程序设计教程赵璐 第三章 课后习题 程序流程控制
  3. ECCV 2020 目标检测论文大盘点(49篇论文)
  4. 淘宝评论爬虫python
  5. 三思推荐的育儿书~~~
  6. module() takes at most 2 arguments (3 given)
  7. Beta版本 为什么程序员总是分不清万圣节和圣诞节?因为 Oct 31 == Dec 25。
  8. 控件(五)——Gridview控件以SqlDataSource控件为数据源实现换肤功能
  9. MySQL中的自增主键用完了怎么办
  10. http status failed,jsp页面加载完了,但是浏览器任然在转圈,页面无法操作