引用

<上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边。试了各种方法都不行。
最后,无意中给RadioButton添加一个backgroud属性即可:
<RadioButton
android:id="@+id/rbAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="测试条目一"
android:textColor="@android:color/primary_text_light"
android:textSize="14sp" />
最后实现了所需效果。>

已检证OK。

上面引用的话,是被用于利用模板xml文件来实现。具体实现模板举例如下:

<RadioGroup

android:layout_width="match_parent"

android:layout_height="warp_content">

<RadioButton

......

/>

......<!- 在这里写下复数个RadioButton ->

</RadioGroup>

这是模板,另外就是动作触发,动作触发元对象任意,比如一个按钮,触发方式很简单,用startActivityForResult()方式。

如果用上面这种模板来做的话,做固定选项可以,不过要应对选项是非固定的内容的话,例如:从数据库里检索出的List时,

就不方便了。这时候就需要在后台去生成RadioButton,并添加到RadioGroup。

Java实现:

java实现的时候RadioGroup是写在页面上的

<RadioGroup

android:id="@+id"

android:layout_width="match_parent"

android:layout_height="warp_content">

</RadioGroup>

在后台动态追加RadioButton。

做法一,代码如下:

     private void initCityGroup(AddressMasterDto addressMasterDto) {
radioGroup.setVisibility(View.VISIBLE);
ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
lvDetailInfo.setVisibility(View.GONE);
resultDtoList = initCityList(addressMasterDto);
if (resultDtoList != null && resultDtoList.size() > 0) {
for (DtoBase dto : resultDtoList) {
AddressMasterDto addressDto = (AddressMasterDto) dto;
if (addressDto != null) {
RadioButton radioButton = new RadioButton(this);
// 其实ID设不设置感觉问题不大,因为本身就不被关心。并且不建议这样设置,为了保证唯一性。
//radioButton.setId(resultDtoList.indexOf(dto) + 1);
radioButton.setText(addressDto.getText());
radioButton.setTextSize(20);
radioButton.setTextColor(getResources().getColor(R.color.black));// 文字颜色android默认是白色~~,我这边背景色是白的,于是我改成黑色的才显示文字。 // 下面这四行是让文字居左,按钮居右。
radioButton.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));// 让原来左边的单选按钮不显示。
Drawable radioDrawable = getResources().getDrawable(android.R.drawable.btn_radio);
radioDrawable.setBounds(0, 0, radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());
radioButton.setCompoundDrawables(null, null, radioDrawable, null); RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
radioLayout.setMargins(-35, 0, 0, 0); // *1 默认match_parent貌似左边并没有到边,剩下的距离自行调整,我这里就用了负数来设定marginLeft来达到靠左。
radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
radioGroup.addView(radioButton); // 这里是在选项间添加了一个分割线。这个做法很好。
if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
View v = new View(this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
radioGroup.addView(v);
}
}
} // 这是为了选中添加监听事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
// 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
// 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
int index = group.indexOfChild(radioButton);
AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
if (extraDto != null) {
returnSearchPage(extraDto);
}
}
});
}
}

当然现在这样做模仿的依然不够彻底,在选择的时候,会发现,spinner会在用2根手指触碰2个选项的时候,只有一个背景色变化,其他选项颜色不变,

看上去就是自动不让你选中了。

而radiogroup是,2个选项会变色,但是只有一个选项被触发选中事件。当然这个只是表现上的区别,其实radiogroup也没错。

接下来就是尝试把radiobutton的样式定义在xml,比如一开始的那样,完了在程序中取得模板,并应用在radiobutton对象中。

做法二,代码如下:

 private void initCityGroup(AddressMasterDto addressMasterDto) {
radioGroup.setVisibility(View.VISIBLE);
ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
lvDetailInfo.setVisibility(View.GONE);
resultDtoList = initCityList(addressMasterDto);
if (resultDtoList != null && resultDtoList.size() > 0) {
for (DtoBase dto : resultDtoList) {
AddressMasterDto addressDto = (AddressMasterDto) dto;
if (addressDto != null) {
RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.spinner_radiobutton, null);
radioButton.setText(addressDto.getText());
RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
radioGroup.addView(radioButton); // 这里是在选项间添加了一个分割线。这个做法很好。
if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
View v = new View(this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
radioGroup.addView(v);
}
}
} // 这是为了选中添加监听事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
// 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
// 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
int index = group.indexOfChild(radioButton);
AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
if (extraDto != null) {
returnSearchPage(extraDto);
}
}
});
}
}
 <?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:textColor="@android:color/black"
android:textSize="20sp" >
</RadioButton>

以上。

RaidoGroup+RadioButton模拟android下拉框弹出List的更多相关文章

  1. div模拟的下拉框特效

    随笔- 4 文章- 0 评论- 0 ? <style type="text/css"> body, ul, li { margin: 0; padding: 0; fo ...

  2. div模拟的下拉框特效jquery

    从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...

  3. android下拉框

    XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  4. 用div,ul,input模拟select下拉框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. jq插件又来了,模拟select下拉框,支持上下方向键哦

    好久没来了,更新下插件, 这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼 (func ...

  6. jquery实现模拟select下拉框效果

    <IGNORE_JS_OP style="WORD-WRAP: break-word"> <!DOCTYPE html PUBLIC "-//W3C// ...

  7. ul -- li 模拟select下拉框

    在写项目中 用到下拉框,一般用 <select name="" id=""> <option value=</option> &l ...

  8. css+html+js实现多级下拉和弹出菜单

    本文将使用css+html+js实现横向菜单.具有多级弹出菜单下拉. 首先我们来看看效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgy ...

  9. 模拟select下拉框、复选框效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

随机推荐

  1. How to setup SVN?

    2014-01-08 11:43:50 如何简单设置SVN(前提是SVN已经安装) 1. 创建一个目录: mkdir -p ~/svn/2.1.J.1.1 2. 进入新创建的目录: cd svn/2. ...

  2. 蓝桥杯 ALGO-4 结点选择 (树形动态规划)

    问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值.如果一个点被选择了,那么在树上和它相邻的点都不能被选择.求选出的点的权值和最大是多少? 输入格式 第一行包含一个整数 n . 接下来的 ...

  3. hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏

    IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...

  4. maven的简单安装与配置

    什么是Maven? Maven可以被理解成"知识的积累",也可以被翻译为"专家".它是一个项目管理工具. 它的主要服务即源于java平台的项目构建.依赖管理和项 ...

  5. winform错误提示 :窗口类名无效(Window class name is not valid)

    winfrom 程序在 xp 操作系统上报错提示 窗口类名无效(Window class name is not valid) 解决方法 注释 Program类 里 这句 Application.En ...

  6. python 接口开发(一)

    cmd中,提示pip版本太低,先升级pip   pip install --upgrade pip (pip,安装和管理python扩展包的工具) cmd下,pip,出现详细信息证明装成功了 pip ...

  7. Tomcat 网站部署(三)

    一.Tomcat的部署方式有以下两种 1.自动部署 2.虚拟目录 二.自动部署 文件必须放在放在webapps就可以了,可以用这样访问 http://localhost:8080/放在webapps目 ...

  8. Core Animation系列之CADisplayLink

    一直以来都想好好学习下CoreAnimation,奈何涉及的东西太多,想要一次性全部搞定时间上不允许,以后会断断续续的补全.最近项目里用到了CADisplayLink,就顺便花点时间看了看. 一.简介 ...

  9. struts调用的几种方法

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  10. 深入理解:Android 编译系统

    一,简介: Android Build 系统是用来编译 Android 系统,Android SDK 以及相关文档的一套框架.众所周知,Android 是一个开源的操作系统.Android 的源码中包 ...