main.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="fill_parent"> <GridView android:id="@+id/myGridView" android:layout_width="fill_parent" android:layout_height="wrap_content" 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:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:padding="3px"/> </LinearLayout>
show_img.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="fill_parent"> <ImageView android:id="@+id/showImg" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
MyGridViewDemo.java:
import java.lang.reflect.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.Gallery.LayoutParams; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SimpleAdapter; public class MyGridViewDemo extends Activity { private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ; private SimpleAdapter simpleAdapter = null; // 适配器 private GridView myGridView = null ; // GridView组件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.myGridView = (GridView) super.findViewById(R.id.myGridView) ; // 取得组件 this.initAdapter() ; // 初始化适配器 this.myGridView.setAdapter(this.simpleAdapter) ; // 设置图片 this.myGridView.setOnItemClickListener(new OnItemClickListenerImpl()) ; } private class OnItemClickListenerImpl implements OnItemClickListener { @SuppressWarnings("unchecked") @Override 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.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // 布局参数 Map<String, Integer> map = (Map<String, Integer>) MyGridViewDemo .this.simpleAdapter.getItem(position); // 取出Map showImg.setImageResource(map.get("img")); // 设置显示图片 Dialog dialog = new AlertDialog.Builder(MyGridViewDemo.this) // 创建Dialog .setIcon(R.drawable.pic_m) // 设置显示图片 .setTitle("查看图片") // 设置标题 .setView(showImg) // 设置组件 .setNegativeButton("关闭", // 设置取消按钮 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { }}).create(); // 创建对话框 dialog.show(); // 显示对话框 } } public void initAdapter(){ // 初始化适配器 Field[] fields = R.drawable.class.getDeclaredFields(); for (int x = 0; x < fields.length; x++) { if (fields[x].getName().startsWith("png_")){ // 所有png_*命名的图片 Map<String,Integer> map = new HashMap<String,Integer>() ; // 定义Map try { map.put("img", fields[x].getInt(R.drawable.class)) ; } catch (Exception e) { // 设置图片资源 } this.list.add(map) ; // 保存Map } } this.simpleAdapter = new SimpleAdapter(this, // 实例化SimpleAdapter this.list, // 要包装的数据集合 R.layout.grid_layout, // 要使用的显示模板 new String[] { "img" }, // 定义要显示的Map的Key new int[] {R.id.img }); // 与模板中的组件匹配 } }