引用

<上面的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. 【STL】-function object

    // Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...

  2. Problem B 队列

    Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...

  3. ModuleWorks免费下载使用方法大全

    ModuleWorks为模拟机器的工具运转及(或)机床和车床材料的搬运提供了一整套解决方案. 模拟技术可以识别潜在的碰撞问题,允许在NC代码生成前进行除错检查,并且渐渐成为CAM处理方面必不可少的解决 ...

  4. RPI学习--wiringpi_API

    reference: https://projects.drogon.net/raspberry-pi/wiringpi/functions/ Functions (API) Some of the ...

  5. ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

    题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...

  6. java删除指定目录及其文件

    import java.io.File; public class Test { public static void main(String args[]){ Test t = new Test() ...

  7. Ubuntu 升级VisualBox后无法启动 Kernel driver not installed (rc=-1908)

    VisualBox之所以在Linux上比传统的VMware快得多,关键一点就是它和Linux内核的结合比较紧密,这也是开源的优点. 不过Linux内核更新很频繁,每次更新内核后启动VirtualBox ...

  8. vijos 1780 开车旅行

    细节巨多. 倍增即可. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  9. Python 练习册--生成唯一激活码(邀请码)

    题目是这样子的: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 分析 其实要生成 ...

  10. C# 对Datatable排序

    一,在C#中要对Datatable排序,可使用DefaultView的Sort方法.先获取Datatable的DefaultView,然后设置 得到的Dataview的sort属性,最后用视图的ToT ...