Android:创建网格状的RadioGroup

Android:创建⽹格状的RadioGroup

Android系统⾃带的RadioGroup只有两种排列⽅式:横向或纵向。但是现实中可能需要将RadioGroup按⽹格状排列,如何实现?本⽂将介绍实现⽅法。

先看效果图:

思路:

1. 创建⼀个PopupWindow的弹出窗⼝

2. 在PopupWindow中填充⼀个GridView

3. 在GridView内填充多个由img和text组合⽽成的、外形类似于RadioButton的组合View视图

4. 当选项有改变的饿时候,更新GridView内的视图。

实现过程:

1. 创建由Img和Text组合⽽成的、外形类似于RadioButton的组合View视图(下⾯简称URadioButton)

先看URadioButton的布局,左图标右⽂字:只需要把图⽚或⽂字填充到<ImageView>或<TextView>中就可以实现内容不同的URadioButton。

item_radiobutton.xml:

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="www.51wendang.com;

android:id="@+id/item_radiobutton"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ImageView android:id="@+id/item_radioimg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

/>

<TextView

android:layout_toRightOf="@+id/item_radioimg"

android:id="@+id/item_radiotext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

/>

</RelativeLayout>

我们的⽬标是把多个这个的URadioButton填充到GridView中,然⽽从GridView的填充函数:

GridView.setAdapter(ListAdapter adapter)

中,可以看出需要将⼀个Adapter填充到GridView中。那么先编写⼀个⽅法:该⽅法能够⽣成由多URadioButton组成的Adapter。

/*

* 创建包含多个radiobutton的Adapter。

* RadioButton的图⽚有redioImg指定,RadioButton的⽂字由radioNameArray指定

* RadioButton的图⽚和⽂字的相对位置在item_radiobutton.xml布局⽂件中指定。

*/

private SimpleAdapter getRadioButtonAdapter(int redioImage, String[] radioNameArray) {

ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

for (int i = 0; i < radioNameArray.length; i++) {

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("itemRadioImg", redioImage);

map.put("itemRadioText", radioNameArray[i]);

data.add(map);

}

SimpleAdapter simperAdapter = new SimpleAdapter(this, data,

R.layout.item_radiobutton, new String[] { "itemRadioImg", "itemRadioText" },

new int[] { R.id.item_radioimg, R.id.item_radiotext });

return simperAdapter;

}

下⾯的代码为:调⽤上⾯定义的getRadioButtonAdapter⽅法⽣成我们所需要Adapter:

Android:创建网格状的RadioGroup相关文档

最新文档

返回顶部