![](http://hi.csdn.net/attachment/201108/31/0_131479296927A3.gif)
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyGridView" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:numColumns="auto_fit" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center"> </GridView> <TextView android:id="@+id/MyTextView" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" android:text="基于GridView的菜单" android:textColor="@android:color/white" android:gravity="center" /> </RelativeLayout>
res/layout/item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/ItemImageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center"/> <TextView android:id="@+id/ItemTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"/> </LinearLayout>
GridViewTestActivity.java
package com.zeph.android.gridview; import
java.util.ArrayList; import
java.util.HashMap; import
java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.Toast; public class GridViewTestActivity extends Activity { private GridView mGridView; private int[] imageRes = { R.drawable.png1, R.drawable.png2, R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6, R.drawable.png7, R.drawable.png8, R.drawable.png9 }; private String[] itemName = { "PNG1", "PNG2", "PNG3", "PNG4", "PNG5", "PNG6", "PNG7", "PNG8", "PNG9" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGridView = (GridView) findViewById(R.id.MyGridView); List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); int length = imageRes.length; for (int i = 0; i < length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImageView", imageRes[i]); map.put("ItemTextView", itemName[i]); data.add(map); } SimpleAdapter mSimpleAdapter = new SimpleAdapter( GridViewTestActivity.this, data, R.layout.item, new String[] { "ItemImageView", "ItemTextView" }, new int[] { R.id.ItemImageView, R.id.ItemTextView }); mGridView.setAdapter(mSimpleAdapter); mGridView.setOnItemClickListener(new GridViewItemOnClick()); } public class GridViewItemOnClick implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), position + "", Toast.LENGTH_SHORT).show(); } } }
图片资源来自:http://preview.zcool.com.cn/preview/new_png/218/