1.传统办法:实现一个继承BaseAdapter的 ImageAdapter
java代码如下:
package com.test;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class GridViewActivity extends Activity { //存放各功能图片 private Integer[] mFunctionPics = {R.drawable.mobilesecurity,R.drawable.infosecurity, R.drawable.no_interrupt,R.drawable.bianjiejieting,R.drawable.xiaomishu ,R.drawable.sms_send_attime,R.drawable.yule,R.drawable.setting}; //存放各功能的名称 private Integer[] mFunctionName = {R.string.phone_tools_box_name1, R.string.phone_tools_box_name2,R.string.phone_tools_box_name3, R.string.phone_tools_box_name4,R.string.phone_tools_box_name5, R.string.phone_tools_box_name6,R.string.phone_tools_box_name7, R.string.phone_tools_box_name8}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview_main); GridView girdview = (GridView) this.findViewById(R.id.girdview); girdview.setAdapter(new ImageAdapter(this)); girdview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(GridViewActivity.this, "图片ID:"+mFunctionPics[position],1).show(); } }); } public class ImageAdapter extends BaseAdapter{ private Context mContext; private LayoutInflater inflater; private class GirdTemp{ ImageView phone_function_pic; TextView phone_function_name; } public ImageAdapter(Context c){ mContext = c; inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return mFunctionPics.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { GirdTemp temp; if(convertView == null){ convertView = inflater.inflate(R.layout.gridview_item, null); temp = new GirdTemp(); temp.phone_function_pic = (ImageView) convertView.findViewById(R.id.function_view); temp.phone_function_name = (TextView) convertView.findViewById(R.id.function_name); convertView.setTag(holder); }else{ temp = (GirdHolder) convertView.getTag(); } temp.phone_function_pic.setImageResource(mFunctionPics[position]); temp.phone_function_name.setText(mFunctionName[position]); return convertView; } }}
girdview_main.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:numColumns="auto_fit" //自动适应多少行
- android:verticalSpacing="20dp" //两行之间的距离
- android:horizontalSpacing="20dp" //两列之间的距离
- android:gravity="center"
- />
girdview_item.xml源码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- <ImageView>
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/function_view"
- android:layout_centerHorizontal="true"
- </ImageView>
- <TextView
- android:layout_width="wrap_content"
- android:layout_below="@+id/function_view"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:id="@+id/function_name">
- </TextView>
- </RelativeLayout>
2.跟ListView类似的数据绑定方式
具体查看hellogv的这篇博客:http://blog.csdn.net/hellogv/article/details/4567095