首页 > 程序开发 > 移动开发 > Android >

Android GridView九宫图

2012-06-20

属性名称描述android:columnWidth设置列的宽度。android:gravity设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizont...

属性名称 描述
android:columnWidth 设置列的宽度。
android:gravity 设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical可以多选,用“|”分开。
android:horizontalSpacing 两列之间的间距。
android:numColumns 设置列数。
android:stretchMode 缩放模式。
android:verticalSpacing 两行之间的间距。


案例一:
1.定义一个布局文件:
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="wrap_content"
04 android:layout_height="wrap_content">
05 <GridView
06 android:id="@+id/gridview_main"
07 android:layout_width="wrap_content"
08 android:layout_height="wrap_content"
09 android:numColumns="auto_fit"
10 android:verticalSpacing="1dp"
11 android:horizontalSpacing="1dp"
12 android:columnWidth="90dp"
13 android:stretchMode="columnWidth"
14 android:gravity="center"
15 >
16 </GridView>
17 </LinearLayout>
android:numColumns="auto_fit"--列数设置为自动;
1 android:columnWidth="90dp"--每列的列宽,也就是item的宽度
1 android:verticalSpacing="1dp"--两行之间的边距
1 android:horizontalSpacing="1dp"--两列之间的边距
1 android:stretchMode="columnWidth"--缩放与列宽大小同步
2。接下来定义
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="wrap_content"
04 android:layout_height="wrap_content"
05 android:orientation="vertical"
06 >
07 <ImageView
08 android:id="@+id/gridview_imageview"
09 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 >
12 </ImageView>
13 <TextView
14 android:id="@+id/gridview_textview"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="TextView01"
18 android:gravity="center"
19 />
20 </LinearLayout>
3.最后定义Java的源代码了,
01 package com.test;
02 import java.util.ArrayList;
03 import java.util.HashMap;
04 import android.app.Activity;
05 import android.os.Bundle;
06 import android.view.View;
07 import android.widget.AdapterView;
08 import android.widget.AdapterView.OnItemClickListener;
09 import android.widget.GridView;
10 import android.widget.SimpleAdapter;
11 public class GridViewDemo extends Activity {
12 int [] image={R.drawable.android,R.drawable.blackberry,R.drawable.windowshone,R.drawable.windowshone,R.drawable.blackberry,R.drawable.android,R.drawable.windowshone,R.drawable.ios,R.drawable.blackberry};
13 private GridView gridView;
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 // TODO Auto-generated method stub
17 super.onCreate(savedInstanceState);
18
19 setContentView(R.layout.gridview_main);
20
21 gridView = (GridView)findViewById(R.id.gridview_main);
22 //生成动态数组,并且转入数据
23 ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
24
25 for(int i=0;i<9;i++)
26 {
27 HashMap<String, Object> map = new HashMap<String, Object>();
28
29 map.put("ItemImage", image[i]);//添加图像资源的ID
30 map.put("itemtext","Amigos_pop-"+String.valueOf(i));
31 list.add(map);
32 }
33 //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
34 SimpleAdapter adapter=new SimpleAdapter(this, //数据来源
35 list,R.layout.gridview_item,//XML实现
36 new String[]{"ItemImage","itemtext"}, //动态数组与ImageItem对应的子项
37 new int[]{R.id.gridview_imageview,R.id.gridview_textview}// //ImageItem的XML文件里面的一个ImageView,两个TextView ID
38 );
39 //添加并且显示
40 gridView.setAdapter(adapter);
41 //添加消息处理
42 gridView.setOnItemClickListener(clickListener);
43 }
44 //当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件
45 private OnItemClickListener clickListener=new OnItemClickListener() {
46
47 @Override
48 public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
49 View arg1,//The view within the AdapterView that was clicked
50 int arg2,//The position of the view in the adapter
51 long arg3//The row id of the item that was clicked
52 )
53 {
54
55 HashMap<String, Object> item=(HashMap<String, Object>)arg0.getItemAtPosition(arg2);
56
57 setTitle((String)item.get("itemtext"));
58 }
59 };
60 }

4:执行效果:




===============================================================================
案例二:同样的效果:
布局文件使用上面一样的:
定义一个ImageAdapter类,此类继承BaseAdapter如下:
01 package com.test;
02
03 import android.content.Context;
04 import android.view.View;
05 import android.view.ViewGroup;
06 import android.widget.BaseAdapter;
07 import android.widget.GridView;
08 import android.widget.ImageView;
09
10 public class GridViewAdapter extends BaseAdapter {
11
12 //定义Context
13 private Context mContext;
14
15 //定义整形数组 (图片资源)
16 private int [] mimages=
17 {
18 R.drawable.android,R.drawable.blackberry,R.drawable.windowshone,
19 R.drawable.windowshone,R.drawable.blackberry,R.drawable.android,
20 R.drawable.windowshone,R.drawable.ios,R.drawable.blackberry
21
22 };
23
24 public GridViewAdapter(Context c)
25 {
26 mContext=c;
27 }
28
29 //获取图片的个数
30 @Override
31 public int getCount() {
32 // TODO Auto-generated method stub
33 return mimages.length;
34 }
35 // 获取图片在库中的位置www.2cto.com
36 @Override
37 public Object getItem(int position) {
38 // TODO Auto-generated method stub
39 return position;
40 }
41
42 //获取图片ID
43 @Override
44 public long getItemId(int position) {
45 // TODO Auto-generated method stub
46 return position;
47 }
48
49 @Override
50 public View getView(int position, View convertView, ViewGroup parent)
51 {
52 ImageView mimageView;
53 if(convertView==null)
54 {
55 //给Imageview设置资源
56 mimageView = new ImageView(mContext);
57 //设置布局图片120*120显示
58
59 mimageView.setLayoutParams(new GridView.LayoutParams(85, 85));
60
61 mimageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
62
63 mimageView.setPadding(8, 8, 8, 8);
64 }else
65 {
66 mimageView=(ImageView) convertView;
67 }
68 mimageView.setImageResource(mimages[position]);
69 return mimageView;
70 }
71 }
01 package com.test;
02 import java.util.ArrayList;
03 import java.util.HashMap;
04 import android.app.Activity;
05 import android.os.Bundle;
06 import android.view.View;
07 import android.widget.AdapterView;
08 import android.widget.AdapterView.OnItemClickListener;
09 import android.widget.GridView;
10 import android.widget.SimpleAdapter;
11 import android.widget.Toast;
12 public class GridViewDemo_2 extends Activity {
13 private GridView gridView;
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 // TODO Auto-generated method stub
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.gridview_main);
19
20 gridView = (GridView)findViewById(R.id.gridview_main);
21
22 gridView.setAdapter(new GridViewAdapter(this));//调用GridViewAdapter
23
24 gridView.setOnItemClickListener(clickListener);
25 }
26
27 private OnItemClickListener clickListener =new OnItemClickListener() {
28
29 @Override
30 public void onItemClick(AdapterView<?> arg0, View arg1, int position,
31 long id) {
32 Toast.makeText(GridViewDemo_2.this, ""+position,Toast.LENGTH_SHORT).show();//显示信息;
33
34 }
35 };
36 }
效果:


作者:amigos_pop




相关文章
最新文章
热点推荐