作者:  发布日期:2014-02-13 21:00:45

0
Tag标签:RadioButton  左侧  显示  
    • 项目中有一个这样的需求:

      下面三行点击某行即选中,颜色变深。自然的想到使用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"?>
      2.<selector xmlns:android="http://schemas.android.com/apk/res/android">
      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:

      01.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      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左侧显示文字,右侧显示按钮时文字不靠边的问题解决的更多相关文章

  1. ifram 实现左侧菜单,右侧显示内容

    一般都是左侧的导航栏中的a标签中写一个target(a标签有target属性), 右侧的div标签中写一个iframe,在iframe中有name的属性,在左侧a标签中的target写上iframe中 ...

  2. C# 窗体 类似framest 左侧点击右侧显示 左侧菜单右侧显示

    首先托一个splitContainer调节大小位置 然后进行再新创建一个窗体名为add 在左侧拖入button按钮 进入代码阶段 更改属性 public Main() { InitializeComp ...

  3. 基于jQuery左侧小图滚动右侧大图显示代码

    今天给大家分享一款 jQuery左侧小图滚动右侧大图显示代码是一款基于jQuery实现的左侧滚动图片点击大图查看效果代码.该实例适用浏览器:IE8.360.FireFox.Chrome.Safari. ...

  4. Yii框架里用grid.CGridView调用pager扩展不显示最后一页按钮的解决

    有如下一例,调用zii.widgets.grid.CGridView显示Blog信息,代码如下: $this->widget('zii.widgets.grid.CGridView', arra ...

  5. C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字

    C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...

  6. easyUI draggable插件使用不当,导致拖动div内部文本框无法输入;设置echarts数据为空时就显示空白,不要动画和文字

    先上一个Demo <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ww ...

  7. 在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字

    在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字呢? 正确的步骤如下: 1, 打开cmd命令行窗口 2, 输入命令 >chcp 65001 数字65001代表的是c ...

  8. jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏

    1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...

  9. ActionBar只显示图标不显示文字

    问题:ActionBar菜单项android:showAsAction设置为android:showAsAction="always|withText"或者android:show ...

随机推荐

  1. java基础(7)--方法

    方法 Math.sqrt() 取平方 Math.pow(2,3) 2的3次方 方法(Method),就是数学函数(Function). 业务方面: 是功能,是动作,一般采用动词命名. 数据层面:是利用 ...

  2. POJ 3977 - subset - 折半枚举

    2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚 ...

  3. Minimum Window Substring, 包含子串的最小窗口,双指针

    问题描述:给定字符串S,子串T,求S中包含T的最小窗口 Given a string S and a string T, find the minimum window in S which will ...

  4. 删除读取失败的服务 <读取描述失败错误代码2>

    1.以管理员身份运行命令提示符 2.sc delete 服务名

  5. javascript onclick 函数不执行

    今天被这个问题搞伤了. 原本是要给table中的元素添加onclick事件处理函数,刚开始还可以,后来就不行了,百试不得其解,反复检查,程序也没问题,耗了一个多小时,偏偏我就不信电脑会扯拐,就要看看还 ...

  6. SSH2 增删查改实例

    (一)引入包 (共73个,不一定都需要,但是我的项目是这么多,经过调试,没有包冲突) (二)创建数据库表 建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄. 语句如下 ...

  7. 使用mybatis如果类属性名和数据库中的属性名不一样取值就会为null

    使用mybatis时如果类属性名和数据库中的属性名不一样取值就会为null 这是不能再去改变javabean中的属性,只能改变sql语句.语句如下所示: <select id="sel ...

  8. javascrip函数简单介绍

    JavaScript 函数定义JavaScript 使用关键字 function 定义函数.函数可以通过声明定义,也可以是一个表达式.函数声明在之前的教程中,你已经了解了函数声明的语法 :functi ...

  9. 7zip 自解压安装程序

    包含自解压安装器的包https://jaist.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920_extra.7z详细说明见7-zip帮助文档的 ...

  10. hdu 5973 Game of Taking Stones(大数,bash game¥)

    Game of Taking Stones Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...