activity_main.xml源码
<GridView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" />
pic_item.xml源码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image" android:layout_width="85dp" android:layout_height="85dp" android:layout_gravity="center" android:scaleType="centerCrop" android:padding="4dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
MainActivity.java
package com.example.gridview; import java.util.ArrayList; import java.util.List; 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.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private GridView gridview; private int[]imgs=new int[]{ R.drawable.grid_view_01,R.drawable.grid_view_02,R.drawable.grid_view_03, R.drawable.grid_view_04,R.drawable.grid_view_05,R.drawable.grid_view_06, R.drawable.grid_view_07,R.drawable.grid_view_08,R.drawable.grid_view_09, R.drawable.grid_view_10,R.drawable.grid_view_11,R.drawable.grid_view_12, R.drawable.grid_view_13,R.drawable.grid_view_14,R.drawable.grid_view_15, R.drawable.grid_view_16,R.drawable.grid_view_17,R.drawable.grid_view_18, }; private String[]tits=new String[]{ "picture_01","picture_02","picture_03", "picture_04","picture_05","picture_06", "picture_07","picture_08","picture_09", "picture_10","picture_11","picture_12", "picture_13","picture_14","picture_15", "picture_16","picture_17","picture_18", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridview=(GridView)findViewById(R.id.gridview); PictureAdapter adapter=new PictureAdapter(tits,imgs,this); gridview.setAdapter(adapter); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?>parent,View v,int position,long id){ if(position<10){ setTitle("单机的图片是:picture_0"+(position+1)); }else{ setTitle("单击的图片是:picture_"+(position+1)); }; } }); } } class PictureAdapter extends BaseAdapter{ private LayoutInflater inflater; private List<Picture>pictures; public PictureAdapter(String[]titles,int[]images,Context context){ super(); pictures=new ArrayList<Picture>(); inflater=LayoutInflater.from(context); for(int i=0;i<images.length;i++) { Picture pic=new Picture(titles[i], images[i]); pictures.add(pic); } } public int getCount(){ if(null!=pictures){ return pictures.size(); }else{ return 0; } } public long getItemId(int position){return position;} public View getView(int position,View convertView,ViewGroup parent){ ViewHolder viewHolder; if(convertView==null){ convertView=inflater.inflate(R.layout.pic_item, null); viewHolder=new ViewHolder(); viewHolder.title=(TextView)convertView.findViewById(R.id.title); viewHolder.image=(ImageView)convertView.findViewById(R.id.image); convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder)convertView.getTag(); } viewHolder.title.setText(pictures.get(position).getTitle()); viewHolder.image.setImageResource(pictures.get(position).getImageId()); return convertView; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } } class ViewHolder{ public TextView title; public ImageView image; } class Picture{ private String title; private int imageId; public Picture() { super(); } Picture(String title,int imageId){ super(); this.title=title; this.imageId=imageId; } public String getTitle(){return title;} public void setTitle(String title){this.title=title;} public int getImageId(){return imageId;} public void setImageId(int imageId){this.imageId=imageId;} }
图片自己加咯~不贴图啦!!