vGridView:用于在界面上按行列分布的方式来显示多个组件。
GridView常用属性
XML属性
|
相关方法
|
说明
|
android:columnWidth
|
setColumnWidth(int)
|
设置列宽度
|
android:gravity
|
setGravity(int)
|
设置对齐方式
|
android:horizontalSpacing
|
setHorizontalSpacing(int)
|
设置各元素间的水平间距
|
android:numColumns
|
setNumColumns(int)
|
设置列数
|
android:stretchMode
|
setStretchMode(int)
|
设置拉伸模式
|
android:verticalSpacing
|
setVerticalSpacing(int)
|
设置各元素间的垂直间距
|
主布局:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <GridView
- android:id="@+id/grid_View"
- android:gravity="center"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:horizontalSpacing="1px"
- android:verticalSpacing="1px"
- android:numColumns="4" >
- </GridView>
- <ImageView
- android:id="@+id/grid_p_w_picpath"
- android:layout_width="280dp"
- android:layout_height="280dp"
- android:src="@drawable/bomb10"
- android:layout_gravity="center_horizontal"/>
- </LinearLayout>
显示每个小图片的布局:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/grid_item"
- android:layout_width="50dp"
- android:layout_height="50dp"/>
- <TextView
- android:id="@+id/grid_id"
- android:layout_below="@+id/grid_item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
同样是用到了simpleadapter类型:
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.SimpleAdapter;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- public class GridViewDemo extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gridview);
- //设置数据list
- List<Map<String, Integer>> list=new ArrayList<Map<String,Integer>>();
- //添加图片id到数组
- final int[] p_w_picpathid=new int[]{
- R.drawable.bomb5,R.drawable.bomb6,R.drawable.bomb7,R.drawable.bomb8,
- R.drawable.bomb9,R.drawable.bomb10,R.drawable.bomb11,R.drawable.bomb12,
- R.drawable.bomb13,R.drawable.bomb14,R.drawable.bomb15,R.drawable.bomb16
- };
- //用循环把图片id添加大数据list中
- for (int i = 0; i < p_w_picpathid.length; i++) {
- Map<String, Integer> aa=new HashMap<String, Integer>();
- aa.put("p_w_picpath", p_w_picpathid[i]);
- list.add(aa);
- }
- //定义simpleadapter
- SimpleAdapter sa=new SimpleAdapter(this, list, R.layout.gridview_2, new String[]{"p_w_picpath","id"}, new int[]{R.id.grid_item,R.id.grid_id});
- GridView iv=(GridView) findViewById(R.id.grid_View);
- iv.setAdapter(sa);
- //选择grid触发的事件.似乎没什么效果
- iv.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- // ImageView p_w_picpathView=(ImageView) findViewById(R.id.grid_p_w_picpath);
- // p_w_picpathView.setImageResource(p_w_picpathid[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- //点击grid事件
- iv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- ImageView p_w_picpathView=(ImageView) findViewById(R.id.grid_p_w_picpath);
- p_w_picpathView.setImageResource(p_w_picpathid[position]);
- }
- });
- }
- }