自定义RadioButton样式,切换按钮时变换背景颜色
来源:互联网 发布:淘宝好评率低于97 编辑:程序博客网 时间:2024/05/11 12:19
先看一下图:
上面这种3选1的效果如何做呢?用代码写?
其实有更简单的办法,忘了RadioButton有什么特性了吗?
我就用RadioButton实现了如上效果,其实很简单的。
首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片:
这里我们只要关心state_checked状态就可以了。所以很简单的配置。
接下来就是布局文件里面引用这张图片了:
注意将android:button="@null"全部设为null,并且将android:background="@drawable/radio"设为刚才新建的图片。这样就完成了。
一句代码都不用写!
看看竖下来的效果:
傍边有图标的怎么办?
也很简单啦,只要在每个RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。
另外要设置图标与文字的距离怎么办?
有一个方法setCompoundDrawablePadding(pad)可以设置图标与文字的距离,对应的属性为android:drawablePadding。
有了这招完全可以实现类似的n选1效果,看下面:
应用:
http://www.iteye.com/topic/1116261#2256664
上面这种3选1的效果如何做呢?用代码写?
其实有更简单的办法,忘了RadioButton有什么特性了吗?
我就用RadioButton实现了如上效果,其实很简单的。
首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_checked="false"
- android:drawable="@drawable/tabswitcher_long" />
- <item
- android:state_checked="true"
- android:drawable="@drawable/tabswitcher_short" />
- </selector>
这里我们只要关心state_checked状态就可以了。所以很简单的配置。
接下来就是布局文件里面引用这张图片了:
- <RadioGroup
- android:gravity="center"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <RadioButton
- android:id="@+id/btn_0"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="最新信息"
- android:textSize="17.0sp"
- android:textColor="@android:color/black"
- android:gravity="center"
- android:layout_weight="1"
- android:checked="true"
- android:button="@null"
- android:background="@drawable/radio"
- />
- <RadioButton
- android:id="@+id/btn_1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="在线专家"
- android:textSize="17.0sp"
- android:textColor="@android:color/black"
- android:gravity="center"
- android:layout_weight="1"
- android:button="@null"
- android:background="@drawable/radio"
- />
- <RadioButton
- android:id="@+id/btn_2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="预约服务"
- android:textSize="17.0sp"
- android:textColor="@android:color/black"
- android:gravity="center"
- android:layout_weight="1"
- android:button="@null"
- android:background="@drawable/radio"
- />
- </RadioGroup>
注意将android:button="@null"全部设为null,并且将android:background="@drawable/radio"设为刚才新建的图片。这样就完成了。
一句代码都不用写!
看看竖下来的效果:
傍边有图标的怎么办?
也很简单啦,只要在每个RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。
另外要设置图标与文字的距离怎么办?
有一个方法setCompoundDrawablePadding(pad)可以设置图标与文字的距离,对应的属性为android:drawablePadding。
有了这招完全可以实现类似的n选1效果,看下面:
应用:
http://www.iteye.com/topic/1116261#2256664
- 自定义RadioButton样式,切换按钮时变换背景颜色
- RadioButton按钮在后,自定义样式效果
- 有关radiobutton自定义按钮样式设置问题
- js实现点击按钮变换背景颜色
- js实现点击按钮变换背景颜色
- android中RadioGroup点击radiobutton切换radiobutton的背景和文字颜色
- 关于中英文切换 切换后按钮背景颜色改变的
- 仿 QQ 底部 Tab 切换带数字提示的 RadioButton,支持自定义提示数字背景颜色、字体大小、字体颜色。后续将持续更新......
- 自定义单选按钮(RadioButton)的样式
- RadioButton和CheckBox自定义按钮图片的样式
- 自定义RadioButton和Checkbox的按钮样式遇到的坑
- 使用RadioButton自定义样式实现喜马拉雅底部的切换功能
- RadioButton - android 自定义RadioButton样式
- 通过颜色矩阵变换按钮按下时候背景
- 自定义RadioButton样式1
- 自定义RadioButton样式
- RadioButton自定义样式
- android 自定义RadioButton样式
- CentOS下用于安装heartbeat/ipvsadm/ldirectord/corosync/pacemaker及相关软件的yum源
- arcgis for javascript定制导航箭头
- netty4.0.x源码分析—event
- PyQt学习笔记(5)——Mian Window
- VMware中ubuntu忘记密码的解决办法[转]
- 自定义RadioButton样式,切换按钮时变换背景颜色
- Nsstring 替换方法
- TPM搭建使用中出现的一点问题
- 题目1371:最小的K个数
- uva 120 Stacks of Flapjacks
- 页面提交后提示“连接被重置”或“载入页面时到服务器的连接被重置”的问题分析
- QT在ui文件上建立信号操机制会不会对后期维护产生影响
- [023] Android平台的信息推送实现 .
- vim中删除技巧