作者:  发布日期: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. 使用C语言扩展Python提供性能

    python底层是用c写的,c本身是一个非常底层的语言,所以它做某些事情的效率肯定会比上层语言高一些. 比如有些自动化测试用的python库,会对系统的UI进行一些捕获,点击之类的操作,这必然要用到c ...

  2. Spring 模块

  3. UVA 725 UVA 10976 简单枚举

    UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...

  4. RAID独立冗余磁盘阵列

    独立冗余磁盘阵列(Redundant Array OF Independent Disks,RAID)开始于20世纪80年代美国加州大学伯克利分校的一个研究项目,当时RAID被称为廉价冗余磁盘阵列(R ...

  5. java接口调试思想

    对于接口调试的理解:最近多次参与接口调试工作,一般情况都是获取对方接口文档,文档中有加密验证方式,根据加密验证方式开发,调用对应的接口.可以不可以简化这个流程那,至少减少一方的工作量.1.减少调用方法 ...

  6. NumPy使用 Matplotlib 绘制直方图

    NumPy - 使用 Matplotlib 绘制直方图 NumPy 有一个numpy.histogram()函数,它是数据的频率分布的图形表示. 水平尺寸相等的矩形对应于类间隔,称为bin,变量hei ...

  7. Pandas排序

    Pandas有两种排序方式,它们分别是 - 按标签 按实际值 下面来看看一个输出的例子. import pandas as pd import numpy as np unsorted_df=pd.D ...

  8. netty同时支持socket和http

    项目需要使用netty做中转服务器,同时支持两种不同协议的客户端,经过几天查询资料终于找到合适的方案了,同时感谢Netty权威指南及论坛问答,开始贴代码 客户端1==>socket public ...

  9. colorlog的三个例子

    例1:默认的log_colors import logging from logging.handlers import RotatingFileHandler from colorlog impor ...

  10. python中*和**的打包和解包

    python中的*和**,能够让函数支持任意数量的参数,它们在函数定义和调用中,有着不同的目的 一. 打包参数 * 的作用:在函数定义中,收集所有的位置参数到一个新的元组,并将这个元组赋值给变量arg ...