(转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决
作者: 发布日期:2014-02-13 21:00:45
项目中有一个这样的需求:
下面三行点击某行即选中,颜色变深。自然的想到使用RadioButton因此决定使用RadioButton和RadioButton实现。
1、RadioButton实现上述效果
01.
<RadioButton
02.
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.
<TextView
10.
android:layout_width=
"wrap_content"
11.
android:layout_height=
"wrap_content"
12.
android:text=
"@string/hello_world"
/>
13.
14.
<RadioGroup
15.
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.
<RadioButton
22.
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.
<RadioButton
33.
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.
<RadioButton
44.
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.
public
class
MainActivity
extends
Activity
implements
OnCheckedChangeListener {
02.
private
RadioGroup rgRight;
03.
private
RadioButton rbAll, rbLimit, rbNone;
04.
05.
@Override
06.
protected
void
onCreate(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.
@Override
18.
public
void
onCheckedChanged(RadioGroup group,
int
checkedId) {
19.
switch
(checkedId) {
20.
case
R.id.rbAll:
21.
radioButtonInit();
22.
rbAll.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
23.
break
;
24.
case
R.id.rbLimit:
25.
radioButtonInit();
26.
rbLimit.setTextColor(getResources().getColor(android.R.color.secondary_text_dark));
27.
break
;
28.
case
R.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.
private
void
radioButtonInit() {
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.
<RadioButton
02.
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.
<RadioGroup
02.
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.
<RadioButton
09.
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.
<RadioButton
20.
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.
<RadioButton
32.
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.
<RadioButton
43.
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.
<RadioButton
54.
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 ...
随机推荐
- 转: 在hibernate中查询使用list,map定制返回类型
在使用hibernate进行查询时,使用得最多的还是通过构建hql进行查询了.在查询的过程当中,除使用经常的查询对象方法之外,还会遇到查询一个属性,或一组聚集结果的情况.在这种情况下,我们通常就需要对 ...
- spark学习12(Wordcount程序之spark-shell)
在目录/home/hadoop/2016113012下有文件words.txt hello scala hello java hello python hello wujiadong 上传该文件到hd ...
- “玲珑杯”ACM比赛 Round #13 B -- 我也不是B(二分排序)
题意:开始有一个空序列s,一个变量c=0,接着从左往右依次将数组a中的数字放入s的尾部,每放一个数字就检测一次混乱度K,当混乱度k大于M时就清空序列并让c=c+1 K = Bi * Vi(1<= ...
- Java数据类型——面试题
1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 +=1;有什么错? 答:对于short s1=1;s1=s1+1来说,在s1+1运算时会自动提升表 ...
- Android -- ContentProvider 内容提供者,创建和调用
1. 概述 ContentProvider 在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentPr ...
- No toolchains found in the NDK toolchains folder for ABI with prefix
通过Android Studio 的Sdk Manager安装NDK,安装完之后编译失败,报错信息如下: No toolchains found in the NDK toolchains folde ...
- ssh整合学习(1)
Hibernate框架 1 hibernate核心配置文件 (0)orm思想 -对象关系映射 (1)数据库信息 (2)hibernate信息 (3)映射配置 (4)hibernate核心配置文件 -如 ...
- python 爬虫003-正则表达式简单介绍
正则表达式,简单的说就是用一个“字符串”来描述一个特征,然后去验证另外一个“字符串”是否符合这个特征. 正则表达式在线测试工具 http://tool.chinaz.com/regex 实例一,判断字 ...
- Django进阶Form篇
一.django表单系统中,所有的表单类都作为django.forms.Form的之类创建,包括ModelForm 关于django的表单系统,主要分两种: 1.基于django.forms.Form ...
- poj3181 背包+大数
http://poj.org/problem?id=3181 Dollar Dayz Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...