在要求不严格的是时候建议使用SimpleAdapter完成。
在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/myGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:numColumns="3"
android:stretchMode="columnWidth"/>
</LinearLayout>
在grid_layout.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="horizontal"
android:background="#000000">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:padding="8px"/>
</LinearLayout>
在MyGridViewDemo.java程序中
package com.tarena.grideview;
import java.text.DateFormat.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SimpleAdapter;
public class MyGridViewDemo extends Activity {
private GridView myGridView = null; //取得组件
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>>list = new ArrayList<Map<String, Integer>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter(); //实例化SimpleAdapter类对象
this.myGridView = (GridView)super.findViewById(R.id.myGridView);
this.myGridView.setAdapter(this.simpleAdapter);
this.myGridView.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ImageView showImg = new ImageView(MyGridViewDemo.this);
showImg.setScaleType(ImageView.ScaleType.CENTER); //图片居中对齐
showImg.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
@SuppressWarnings("unchecked")
Map<String, Integer> map = (Map<String, Integer>)parent.getAdapter().getItem(position);
showImg.setImageResource(map.get("img")); //设置显示图片
Dialog dialog = new AlertDialog.Builder(MyGridViewDemo.this)
.setIcon(R.drawable.ic_launcher).setTitle("查看图片")
.setView(showImg).setNegativeButton("关闭",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create();
dialog.show();
}
}
private void initAdapter(){
java.lang.reflect.Field[] fields = R.drawable.class.getDeclaredFields();
for(int x = 0;x < fields.length; x++){
if(fields[x].getName().startsWith("ispic_")){
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img",fields[x].getInt(R.drawable.class));
} catch (Exception e) {
e.printStackTrace();
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this,this.list,
R.layout.gird_layout,new String[]{"img"},
new int[]{R.id.img});
}
}