package org.wangliang.gridview;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.widget.GridView;import android.widget.SimpleAdapter;import android.widget.Toast;public class Main extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.gridview);        setupAdapter();    }private void setupAdapter() {// TODO Auto-generated method stubGridView gridView = (GridView) findViewById(R.id.emotions);ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();Field[] fileds = R.drawable.class.getDeclaredFields();for(Field f:fileds) {if(!f.getName().equals("icon")) {int id;try {id = f.getInt(null);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(this, "wrong!", 1).show();return ;} HashMap<String,Object> map = new HashMap<String,Object>();map.put("image", id);//这里使用的是资源的iddata.add(map);}}SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.gridview_item, new String[] {"image"}, new int[] {R.id.emotion_item});gridView.setAdapter(adapter);}}

//下面是布局文件gridview..xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><GridViewandroid:id="@+id/emotions"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scrollbars="vertical"android:numColumns="auto_fit"android:verticalSpacing="10dp"android:stretchMode="columnWidth"android:gravity="center"android:columnWidth="40dp"></GridView></LinearLayout>
//下面是布局文件gridview_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/emotion_item"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_weight="50"android:layout_gravity="center"></ImageView></LinearLayout>



更多相关文章

  1. android文件管理器--界面效果二(layout)
  2. android 颜色大全 color文件
  3. 实用正则表达式扫描android SDcard的文件
  4. ToastUtil
  5. Layer_list(层叠图片)
  6. SwipeRefreshLayout使用
  7. How to Use Android(安卓)ADB Command Line Tool
  8. scrollView 在中间,上下各有一个固定控件的布局
  9. [Android][工具类]FileUtils

随机推荐

  1. android选取照片并以其uri取得绝对路径
  2. 自定义Tab1
  3. 完全卸载 Android Studio
  4. [转]Why Android isn’t ready for TDD,
  5. android adb 文章
  6. android获取sd卡路径方法
  7. AndEngine绘制滚动文本TickerText
  8. Android 中Fragment实现(芝麻虽小,必不可
  9. ANDROID获取Task及Process
  10. android之android.os.NetworkOnMainThrea