main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchMode="columnWidth" android:verticalSpacing="5dip" android:horizontalSpacing="5dip" android:numColumns="3" /> </RelativeLayout>
gridview_item.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_height="100dip" android:layout_width="150dip" android:scaleType="fitXY" /> <TextView android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity如下:
import java.util.ArrayList; import java.util.HashMap; 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.TextView; import android.widget.Toast; import android.app.Activity; import android.content.res.TypedArray; /** *Demo描述:利用GridView展示图片 *注意: *图片的id是存放在array.xml中的,注意TypedArray的使用 */ public class MainActivity extends Activity { private GridView mGridView; TypedArray photoIDArray=null; private String [] photoNameArray=null; private ArrayList<HashMap<String, Object>> gridViewItemsArrayList; private HashMap<String, Object> everyItemHashMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mGridView=(GridView) findViewById(R.id.gridView); photoIDArray=getResources().obtainTypedArray(R.array.photoIDArray); photoNameArray=getResources().getStringArray(R.array.photoNameArray); gridViewItemsArrayList=new ArrayList<HashMap<String,Object>>(); for (int i = 0; i < photoNameArray.length; i++) { everyItemHashMap=new HashMap<String, Object>(); everyItemHashMap.put("photoID", (int)photoIDArray.getResourceId(i,0)); everyItemHashMap.put("photoName", photoNameArray[i]); gridViewItemsArrayList.add(everyItemHashMap); } SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this, gridViewItemsArrayList, R.layout.gridview_item, new String []{"photoID","photoName"}, new int[]{R.id.imageView,R.id.textView}); mGridView.setAdapter(simpleAdapter); mGridView.setOnItemClickListener(new ItemClickListenerImpl()); } private class ItemClickListenerImpl implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { TextView textView=(TextView) view.findViewById(R.id.textView); String name=textView.getText().toString(); Toast.makeText(MainActivity.this, position+" "+name, Toast.LENGTH_SHORT).show(); } } }