引用

<上面的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. HDU5478 原根求解

    看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...

  2. android 录音的断点续传

    系统没有暂停的功能  只能把每次的录音进行拼接... package com.example.zrecord; import java.io.File;import java.io.FileInput ...

  3. [转]ps/2键盘线序识别方法

    from: http://www.360doc.com/content/11/0816/19/844619_140875056.shtml 经常看到有人询问ps/2线坏了,更换的时候如何测线序连线,或 ...

  4. 统计一段文字中出现频率最高的10个单词(c语言)

    注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...

  5. julia解无忧公主的数学时间097.jl

    julia解无忧公主的数学时间097.jl #=""" julia解无忧公主的数学时间097.jl http://mp.weixin.qq.com/s?__biz=MzI ...

  6. 转:115个Java面试题和答案——终极列表(上)

    转自:http://www.importnew.com/10980.html 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的 ...

  7. oracle 删除表和数据分析语句

    TRUNCATE TABLE 表名;ANALYZE TABLE 表名 ESTIMATE STATISTICS;

  8. spark与Hadoop区别

    2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 )   谈到大数据,相信大家对Hadoop和Apache Spark ...

  9. hdu 2058

    PS:TLE了N次...虽然结果对了...后来看了公式才知道要枚举项数才行... 代码: #include "stdio.h"#include "math.h" ...

  10. KochSnow曲线

    在这里实现了Koch曲线,而且提到我们只需要对一个等边三角形的各条边按照Koch曲线的算法进行绘图就能得到KochSnow曲线,将其实现到之前提到的绘图框架中,考虑到KochSnow的实现主要依赖Ko ...