Gridview使用 Main.xml源代码如下,Gridview 用来装载Item. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android rientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text" /> <GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="auto_fit" android:columnWidth=”90dp” android:stretchMode="columnWidth" android:horizontalSpacing="5dp" android:verticalSpacing="5dp" ></GridView> </LinearLayout> 介绍一下里面的某些属性: android:numColumns="auto_fit" ,GridView的列数设置为自动 android:columnWidth="90dp",每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth",缩放与列宽大小同步
android:verticalSpacing="5dp",两行之间的边距 android:horizontalSpacing="5dp",两列之间的边距 接下来介绍一下cell.xml <?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 rientation="horizontal" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout> 最后是JAVA源代码: MainActivity.java package com.example.gridview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.SimpleAdapter; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener{ private GridView gridView; private int[] resIds = new int[]{ R.drawable.image1,R.drawable.image2,R.drawable.image3, R.drawable.image4,R.drawable.image5,R.drawable.image6, R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,R.drawable.image12,R.drawable.image13,R.drawable.image14,R.drawable.image15,R.drawable.image15};//这里是实现准备好的八张图片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView = (GridView)this.findViewById(R.id.gridview); //--生成动态数组,并转入数据--// List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < resIds.length; i++) { Map<String, Object> cell = new HashMap<String, Object>(); cell.put("imageview", resIds);//添加图片资源的ID list.add(cell); } SimpleAdapter simpleAdapter = new SimpleAdapter( this, list, //数据来源 R.layout.cell, //cell的XML实现 //--动态数组与list的对应子项--// new String[]{"imageview"}, new int[]{R.id.imageview}); //--添加并显示--// gridView.setAdapter(simpleAdapter); //--添加消息处理--// gridView.setOnItemClickListener(this); } //--当你点击item时,事件被处罚--// //--屏幕显示你点击了第几个Item--// @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你点击了第"+(position+1)+”个Item”, Toast.LENGTH_LONG).show(); } } 这个添加事件:你可以添加一个switch语句来完成点击事件, Switch(position){ case 0: Toast.makeText(MainActivity.this, "11", Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(MainActivity.this, "22",Toast.LENGTH_LONG).show(); Intent intent1 = new Intent(); break; case 2: Toast.makeText(MainActivity.this, "33",Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(MainActivity.this, "44",Toast.LENGTH_LONG).show(); break; case 4: Toast.makeText(MainActivity.this, "55", Toast.LENGTH_LONG).show(); break; default: break;} 效果图:
|