ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似。CompoundButton有两个状态,分别是checked和not checked。Switch是android4.0后出现的控件。但是这几个组件的默认图标都不太好看,怎样设置自己的图标风格呢?以下就是我的一种解决方案。

先看一下效果图,如下:

按钮图片贡献如下:

      

实现过程:

1.建立/res/drawable/setting_checkbox_button.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.  
  4. <item android:drawable="@drawable/on_button" android:state_checked="true"/>
  5. <item android:drawable="@drawable/off_button" android:state_checked="false"/>
  6. </selector>

2、在values/styles.xml中添加如下代码:

  1. <style name="MyToggleButton" parent="@android:style/Widget.CompoundButton">
  2. <item name="android:button">@drawable/setting_checkbox_button</item>
  3. </style>

3.layout布局文件/res/layout/togglebutton_switch2.xml如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:id="@+id/layout1"
  6. android:gravity="center_horizontal">
  7.  
  8. <TextView
  9. android:id="@+id/text1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentTop="true"
  13. android:gravity="center"
  14. android:text="toggle is checked..." />
  15.  
  16. <ToggleButton
  17. android:id="@+id/togglebutton"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/text1"
  21. android:background="#00000000"
  22. style="@style/MyToggleButton"
  23. android:checked="true"
  24. android:textOff="关闭"
  25. android:textOn="打开" />
  26.  
  27. <Switch
  28. android:id="@+id/witch"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:gravity="center"
  32. android:layout_below="@id/togglebutton"
  33. android:layout_alignLeft="@id/togglebutton"
  34. android:checked="true"
  35. android:textOff="关闭"
  36. android:textOn="打开"
  37. android:thumb="@drawable/tb_thumb"
  38. android:track="@drawable/tb_track" />
  39.  
  40. <CheckBox
  41. android:id="@+id/check"
  42. style="@style/MyToggleButton"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@id/witch"
  46. android:checked="true"
  47. android:textOff="关闭"
  48. android:textOn="打开" />
  49.  
  50. <RadioButton
  51. android:id="@+id/redio"
  52. style="@style/MyToggleButton"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:layout_below="@id/check"
  56. android:checked="false"
  57. android:textOff="关闭"
  58. android:textOn="打开" />
  59.  
  60. <ToggleButton
  61. android:id="@+id/togglebutton2"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:button="@drawable/compound_button"
  65. android:layout_below="@id/redio"
  66. android:layout_alignLeft="@id/redio"
  67. android:visibility="gone"/>
  68.  
  69. <FrameLayout
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_below="@id/redio"
  73. android:layout_gravity="center"
  74. android:layout_alignLeft="@id/redio">
  75.  
  76. <Switch
  77. android:id="@+id/witch"
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:layout_gravity="left"
  81. android:checked="true"
  82. android:textOff="关闭"
  83. android:textOn="打开"/>
  84. </FrameLayout>
  85.  
  86. </RelativeLayout>

以上写的还有一问题,就是最后控件RadioButton点不了;还有就是把android:layout_width="wrap_content"改成android:layout_width="match_parent"布局会出错。可能这个方法还不太好,我也是在学习中,欢迎大家一起讨论,有更好的实现方式请一定告诉我哦!

有一个网友写的可以整个项目控制ToggleButton的风格,我看了一下,第2步
设置Style & Theme的地方看不太懂,写
/res/drawable/themes.xml这个文件时在我这里会报错。大家可以参考一下:
http://blog.csdn.net/billpig/article/details/6634481

设置ToggleButton、Switch、CheckBox和RadioButton的显示效果的更多相关文章

  1. MVC小系列(十八)【给checkbox和radiobutton添加集合的重载】

    mvc对DropDownListFor的重载很多,但对checkbox和radiobutton没有对集合的重载 所以该讲主要针对集合的扩展: #region 复选框扩展 /// <summary ...

  2. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  3. Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码

    随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox. Radiobutton . DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的 ...

  4. Android零基础入门第20节:CheckBox和RadioButton使用大全

    原文:Android零基础入门第20节:CheckBox和RadioButton使用大全 本期先来学习Button的两个子控件,无论是单选还是复选,在实际开发中都是使用的较多的控件,相信通过本期的学习 ...

  5. 转:zTree树控件入门之checkbox:如何动态设置节点的checkbox选择框启用与禁用状态(chkDisabled)

    当一棵树的部分节点根据登入用户角色不同而决定是否启用节点前的checkbox选择框的时候,我们应该如何做呢?也或者如何在页面加载的时候动态根据当前登入用户角色动态切换节点前的checkbox的禁用状态 ...

  6. CheckBox和RadioButton以及RadioGroup

    CheckBox:复选框 有两种状态 选中状态(true),未选状态(false) 属性 android:checked= "false"(表示该复选框未被选中) RadioGro ...

  7. 【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)

    3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html Edi ...

  8. Android UI系列-----CheckBox和RadioButton(1)

    主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...

  9. android基本控件学习-----ToggleButton&Switch

    ToggleButton(开关按钮)和Switch(开关)讲解: 一.核心属性讲解: (1)ToggleButton textOn:按钮被选中的时候文字显示 textOff:按钮没有被选中的时候文字显 ...

随机推荐

  1. Linux05--Shell程序设计01

    1.Shell脚本介绍 基本介绍: shell脚本是一个可执行的纯文本文件,由多个shell命令组成. 命令的执行是从上而下,从左而右的分析和执行 命令,参数间的多个空白也会被忽略 #是注释 #!用于 ...

  2. JLINK烧写BIN文件到nand、norflash、SDRAM

    1. 简要说明 JLink的调试功能.烧写Flash的功能都很强大,但是对于S3C2410.S3C2440的Flash操作有些麻烦:烧写Nor Flash时需要设置SDRAM,否则速率很慢:烧写Nan ...

  3. Entity Framework学习笔记

    原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总   之前的小项目做完了,到了总结经验和 ...

  4. error: C2664: “zajiao::zajiao(const zajiao &)”: 无法将参数 1 从“const char [12]”转换为“char *”

    原本打算在QT用一个字符串"ABCDEF12345"作为类zajiao的构造函数的参数,用来创建类zajiao的对象zajiao1. zajiao zajiao1("AB ...

  5. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  6. andengine游戏引擎总结进阶篇1

    本篇包括虚拟键盘,粒子系统 1虚拟键盘 分为两种,一种是单个虚拟键盘,另一种是多个方位虚拟键盘 1)加载虚拟键盘所需要的图片资源 private BitmapTextureAtlas mOnScree ...

  7. andengine游戏引擎总结进阶篇2

    本篇包括瓦片地图,物理系统, 1瓦片地图 超级玛丽,冒险岛,魂斗罗等游戏主场景都有瓦片地图画成,它的作用可见一斑,它可以用tiled Qt软件画成,在辅助篇中讲讲解tiled Qt软件的使用 1)加载 ...

  8. 重写系统中的UINavigationController返回按钮的事件

    1.扩展UIviewController UIViewController+BackButtonHandler.h #import <UIKit/UIKit.h> @protocol Ba ...

  9. C语言中为什么不能把char**赋给const char**

    这是我在知乎回答的一个问题. 这个问题是C中的一个深坑,首先说结论: char ** 和 const char ** 是两个不相容(incompatible)的类型,能够理解为不能直接赋值 在C11的 ...

  10. div内嵌p,div等块元素出现的问题

    div内嵌p,div等块元素出现的问题 http://caiceclb.iteye.com/blog/428085 div内部块级元素,比如p,div,设置外间距(margin)的话会怎样.本来还纳闷 ...