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

先看一下效果图,如下:

按钮图片贡献如下:

      

实现过程:

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1"
android:gravity="center_horizontal"> <TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="toggle is checked..." /> <ToggleButton
android:id="@+id/togglebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
android:background="#00000000"
style="@style/MyToggleButton"
android:checked="true"
android:textOff="关闭"
android:textOn="打开" /> <Switch
android:id="@+id/witch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_below="@id/togglebutton"
android:layout_alignLeft="@id/togglebutton"
android:checked="true"
android:textOff="关闭"
android:textOn="打开"
android:thumb="@drawable/tb_thumb"
android:track="@drawable/tb_track" /> <CheckBox
android:id="@+id/check"
style="@style/MyToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/witch"
android:checked="true"
android:textOff="关闭"
android:textOn="打开" /> <RadioButton
android:id="@+id/redio"
style="@style/MyToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/check"
android:checked="false"
android:textOff="关闭"
android:textOn="打开" /> <ToggleButton
android:id="@+id/togglebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/compound_button"
android:layout_below="@id/redio"
android:layout_alignLeft="@id/redio"
android:visibility="gone"/> <FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/redio"
android:layout_gravity="center"
android:layout_alignLeft="@id/redio"> <Switch
android:id="@+id/witch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="true"
android:textOff="关闭"
android:textOn="打开"/>
</FrameLayout> </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. linux查看网卡吞吐量和网卡流量用自带命令,iptraf查看。

    1 命令行直接输入:iptraf(如果没有,使用yum install iptraf安装) 2.打开进入选择ip镜像. 3. 选择监听测试的接口,所有接口. 4.开启监听状态日志,系统默认日志路径./ ...

  2. Example: Develop Web application on Baidu App Engine using CherryPy

    In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...

  3. SSM整合中遇到的不能扫描注解的bug

    我的开发环境为: ubuntu14.04LTS 64bit; Spring Tool Suite  3.5.0.RELEASE Maven 3.2.3 SSM整合中遇到的不能扫描注解的bug 最终解决 ...

  4. Docker 基于已有镜像的容器创建镜像

    Docker 基于已有镜像的容器创建镜像: docker:/root# docker run -it januswel/centos /bin/bash docker exec -it januswe ...

  5. tracker-store and tracker-miner-fs eating up my CPU on every startup

    Tracker is a synergy of technologies that are designed to provide a highly sophisticated, innovative ...

  6. JVM(Java虚拟机)优化大全和案例实战

    堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  7. aix光盘安装包 aix puppet agent 自动化安装

    脚本待处理事务1,替换指定行数据2,获取$1 :字段分割 [Tips Notes,byRui]从光盘等安装媒介中isntallp -l -d /opt/ruiyhe or /dev/cd0 搜索所有的 ...

  8. python升级导致的坑

    问题来源 问题往往都是这样来的突然,让我措手不及. 小孩没娘说来话长啊,操作系统是centos6.5因此默认自带的python是2.6.6的,突然有一天我要写一个关于kafka topic消费情况的监 ...

  9. Gunner II(二分,map,数字转化)

    Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  10. 微软将Bing变开放平台 同谷歌争夺开发者

    微软在编译者大会上宣布将Bing作为平台开放,此举显然旨在改变谷歌(微博)一家独大的局面. 报道称,微软知道如何创建平台.因此当它发布新平台时,都值得业界仔细关注.就在上周之前,微软Bing给大家的印 ...