(转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决
作者: 发布日期:2014-02-13 21:00:45
项目中有一个这样的需求:

下面三行点击某行即选中,颜色变深。自然的想到使用RadioButton因此决定使用RadioButton和RadioButton实现。
1、RadioButton实现上述效果
01.<RadioButton02.android:id="@+id/rbAll"03.android:layout_width="match_parent"04.android:layout_height="wrap_content"05.android:button="@null"06.android:drawableRight="@drawable/selector_tb"07.android:text="测试条目一"08.android:textColor="@android:color/primary_text_light"09.android:textSize="14sp"/>这个是RadioButton的实现:首先android:button="@null",这个用于隐藏RadioButton默认的按钮;android:drawableRight="@drawable/selector_tb"这个用于显示自己定义的按钮,也就是上图右侧的按钮,当然,如果在左侧,你可以设置android:drawableLeft="@drawable/selector_tb"等;然后就是文字的属性。这样就设置完毕了。android:drawableRight="@drawable/selector_tb"中的selector_tb是一个选择器,代码如下:
1.<?xml version="1.0"encoding="utf-8"?>3.<item android:drawable="@drawable/ic_rb_press"android:state_checked="true"/>4.<item android:drawable="@drawable/ic_rb_unpress"android:state_checked="false"/>5.</selector>完整的代码如下:
布局文件activity_main.xml:
03.android:layout_width="match_parent"04.android:layout_height="match_parent"05.android:orientation="vertical"06.android:background="@android:color/white"07.tools:context=".MainActivity">08.09.<TextView10.android:layout_width="wrap_content"11.android:layout_height="wrap_content"12.android:text="@string/hello_world"/>13.14.<RadioGroup15.android:id="@+id/rgRight"16.android:layout_width="match_parent"17.android:layout_height="wrap_content"18.android:layout_marginTop="8dp"19.android:orientation="vertical">20.21.<RadioButton22.android:id="@+id/rbAll"23.android:layout_width="match_parent"24.android:layout_height="wrap_content"25.android:background="@android:color/white"26.android:button="@null"27.android:drawableRight="@drawable/selector_tb"28.android:text="测试条目一"29.android:textColor="@android:color/primary_text_light"30.android:textSize="14sp"/>31.32.<RadioButton33.android:id="@+id/rbLimit"34.android:layout_width="match_parent"35.android:layout_height="wrap_content"36.android:background="@android:color/white"37.android:button="@null"38.android:drawableRight="@drawable/selector_tb"39.android:text="测试条目二"40.android:textColor="@android:color/primary_text_light"41.android:textSize="14sp"/>42.43.<RadioButton44.android:id="@+id/rbNone"45.android:layout_width="match_parent"46.android:layout_height="wrap_content"47.android:button="@null"48.android:drawableRight="@drawable/selector_tb"49.android:text="测试条目三"50.android:textColor="@android:color/primary_text_light"51.android:textSize="14sp"/>52.</RadioGroup>53.54.</LinearLayout>MainActivity.java:
01.publicclassMainActivityextendsActivityimplementsOnCheckedChangeListener {02.privateRadioGroup rgRight;03.privateRadioButton rbAll, rbLimit, rbNone;04.05.@Override06.protectedvoidonCreate(Bundle savedInstanceState) {07.super.onCreate(savedInstanceState);08.setContentView(R.layout.activity_main);09.10.rgRight = (RadioGroup) findViewById(R.id.rgRight);11.rgRight.setOnCheckedChangeListener(this);12.rbAll = (RadioButton) findViewById(R.id.rbAll);13.rbLimit = (RadioButton) findViewById(R.id.rbLimit);14.rbNone = (RadioButton) findViewById(R.id.rbNone);15.}16.17.@Override18.publicvoidonCheckedChanged(RadioGroup group,intcheckedId) {19.switch(checkedId) {20.caseR.id.rbAll:21.radioButtonInit();22.rbAll.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));23.break;24.caseR.id.rbLimit:25.radioButtonInit();26.rbLimit.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));27.break;28.caseR.id.rbNone:29.radioButtonInit();30.rbNone.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));31.break;32.default:33.break;34.}35.}36.37.privatevoidradioButtonInit() {38.rbAll.setTextColor(getResources().getColor(android.R.color.primary_text_light));39.rbLimit.setTextColor(getResources().getColor(android.R.color.primary_text_light));40.rbNone.setTextColor(getResources().getColor(android.R.color.primary_text_light));41.}42.43.}2、问题
上面的代码运行后效果如下:

上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边。试了各种方法都不行。
最后,无意中给RadioButton添加一个backgroud属性即可:
01.<RadioButton02.android:id="@+id/rbAll"03.android:layout_width="match_parent"04.android:layout_height="wrap_content"05.android:background="@android:color/white"06.android:button="@null"07.android:drawableRight="@drawable/selector_tb"08.android:text="测试条目一"09.android:textColor="@android:color/primary_text_light"10.android:textSize="14sp"/>最后实现了所需效果。
3、总结
虽然效果实现了,但是这个问题一直不明白为什么,怀疑可能是RadioButton没有真正的match_parent。因此写了下面的测试代码:
01.<RadioGroup02.android:id="@+id/rgRight"03.android:layout_width="match_parent"04.android:layout_height="wrap_content"05.android:layout_marginTop="8dp"06.android:orientation="vertical">07.08.<RadioButton09.android:id="@+id/rbAll"10.android:layout_width="match_parent"11.android:layout_height="wrap_content"12.android:layout_margin="4dp"13.android:button="@null"14.android:drawableRight="@drawable/selector_tb"15.android:text="测试条目一"16.android:textColor="@android:color/primary_text_light"17.android:textSize="14sp"/>18.19.<RadioButton20.android:id="@+id/rbAll"21.android:layout_width="match_parent"22.android:layout_height="wrap_content"23.android:layout_margin="4dp"24.android:paddingLeft="0dp"25.android:button="@null"26.android:drawableRight="@drawable/selector_tb"27.android:text="测试条目一"28.android:textColor="@android:color/primary_text_light"29.android:textSize="14sp"/>30.31.<RadioButton32.android:id="@+id/rbLimit"33.android:layout_width="match_parent"34.android:layout_height="wrap_content"35.android:button="@null"36.android:drawableRight="@drawable/selector_tb"37.android:paddingLeft="4dp"38.android:text="测试条目二"39.android:textColor="@android:color/primary_text_light"40.android:textSize="14sp"/>41.42.<RadioButton43.android:id="@+id/rbNone"44.android:layout_width="match_parent"45.android:layout_height="wrap_content"46.android:button="@null"47.android:drawableRight="@drawable/selector_tb"48.android:padding="0dp"49.android:text="测试条目三"50.android:textColor="@android:color/primary_text_light"51.android:textSize="14sp"/>52.53.<RadioButton54.android:id="@+id/rbNone"55.android:layout_width="match_parent"56.android:layout_height="wrap_content"57.android:background="@android:color/white"58.android:button="@null"59.android:drawableRight="@drawable/selector_tb"60.android:text="测试条目三"61.android:textColor="@android:color/primary_text_light"62.android:textSize="14sp"/>63.</RadioGroup>运行后效果如下:

通过对比,感觉应该是左面有padding。但是,为什么设置background也行呢?高手请指点,谢谢~~
(转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决的更多相关文章
- ifram 实现左侧菜单,右侧显示内容
一般都是左侧的导航栏中的a标签中写一个target(a标签有target属性), 右侧的div标签中写一个iframe,在iframe中有name的属性,在左侧a标签中的target写上iframe中 ...
- C# 窗体 类似framest 左侧点击右侧显示 左侧菜单右侧显示
首先托一个splitContainer调节大小位置 然后进行再新创建一个窗体名为add 在左侧拖入button按钮 进入代码阶段 更改属性 public Main() { InitializeComp ...
- 基于jQuery左侧小图滚动右侧大图显示代码
今天给大家分享一款 jQuery左侧小图滚动右侧大图显示代码是一款基于jQuery实现的左侧滚动图片点击大图查看效果代码.该实例适用浏览器:IE8.360.FireFox.Chrome.Safari. ...
- Yii框架里用grid.CGridView调用pager扩展不显示最后一页按钮的解决
有如下一例,调用zii.widgets.grid.CGridView显示Blog信息,代码如下: $this->widget('zii.widgets.grid.CGridView', arra ...
- C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字
C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...
- easyUI draggable插件使用不当,导致拖动div内部文本框无法输入;设置echarts数据为空时就显示空白,不要动画和文字
先上一个Demo <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ww ...
- 在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字
在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字呢? 正确的步骤如下: 1, 打开cmd命令行窗口 2, 输入命令 >chcp 65001 数字65001代表的是c ...
- jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏
1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...
- ActionBar只显示图标不显示文字
问题:ActionBar菜单项android:showAsAction设置为android:showAsAction="always|withText"或者android:show ...
随机推荐
- java jvm内存管理/gc策略/参数设置
1. JVM内存管理:深入垃圾收集器与内存分配策略 http://www.iteye.com/topic/802638 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想 ...
- Jar 包 及运行Jar包 - 转载
Eclipse的jar file和Runnable JAR file的区别 - 及bat运行Runnable JAR文件 1.两种jar的区别 jar file是最普通的jar包,即平时我们工程中li ...
- MQ 个人小结
在PCS项目: talking 发送队列1.1 创建@Beanpublic Queue orderTakingQueue() { return createQueue(orderTakingQueue ...
- Windows 10 操作系统删除Administrator登录选项
在win10操作系统锁屏界面登录选项中突然出现了两个用户选项,一个是自己微软账户登录的用户,另一个是Administrator登录选项,每次看到好别扭,怎么去掉呢? 一.以管理员身份运行命令提示符(c ...
- PermutationSequence,求第k个全排列
问题描述:给定一个数组,数组里面元素不重复,求第k个全排列. 算法分析:这道题就是用到取商取模运算. public String getPermutation(int n, int k) { // i ...
- UOJ34 多项式乘法(非递归版)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- kindeditor上传本地图片实例
所需插件:kindeditor下载 密码: 5ry4 jsp文件: <script type="text/javascript" language="javas ...
- appium自动化测试(五)
1. 页面封装——理性判断 2. basepage——定位表达式的判断——要加上移动端的,加上上下左右滑动封装.toast可以封装.webview切换操作 3. 页面当中,所有元素定位——更换——移动 ...
- 快速掌握Android 虚拟机(AVD)方法
摘自 http://jingyan.baidu.com/article/15622f24707710fdfdbea553.html 参阅:http://jingyan.baidu.com/articl ...
