mainActivity.java 两种显示方式,第一种图片和对应的文字,第二中只显示图片 package com.soft2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private int [] imageId=new int []{ R.drawable.img01,R.drawable.img02,R.drawable.img03, R.drawable.img04,R.drawable.img05,R.drawable.img06, R.drawable.img07,R.drawable.img08,R.drawable.img09, R.drawable.img10,R.drawable.img11,R.drawable.img12, }; //定义并初始化图片id的数组 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取GridView组件 GridView gridView=(GridView)findViewById(R.id.gridView1); /*使用simpleadapter指定要显示的内容*/ //定义并初始化文字内容数组 String [] title=new String []{ "花开富贵", "海天一色", "日出", "天路", "一枝独秀", "云", "独占鳌头", "蒲公英花", "花团锦簇", "争奇斗艳", "和谐", "林间小路" }; List<Map<String,Object>> listItems=new ArrayList<Map<String, Object>>(); //通过for循环将图片id和列表项文字放到map中,并添加到list集合中 for(int i=0;i<imageId.length;i++){ Map<String, Object> map=new HashMap<String, Object>(); map.put("imageView1", imageId[i]); map.put("title", title[i]); listItems.add(map);//将map对象添加到List集合中 } SimpleAdapter adapter=new SimpleAdapter(this,listItems, R.layout.items,new String[]{ "title","imageView1"}, new int []{R.id.textView1,R.id.imageView1});//创建SimpleAdapter gridView.setAdapter(adapter);//将适配器与gridView关联 /*BaseAdapter adapter=new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView;//声明ImageView的对象 if(convertView==null){ imageView=new ImageView(MainActivity.this);//实例化imageView的对象 imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);//设置缩放方式 imageView.setPadding(5, 0, 5, 0);//设置imageview的内边框边距 }else{ imageView=(ImageView)convertView; } imageView.setImageResource(imageId[position]);//imageView设置要显示的图片 return imageView; } //获得当前选项的ID @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public int getCount() { // TODO Auto-generated method stub return imageId.length; } }; gridView.setAdapter(adapter);//将适配器与gridView关联 */ } }
activity_main <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchMode="columnWidth" android:numColumns="1" > </GridView> </RelativeLayout>
items.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:paddingLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter"/> <TextView android:id="@+id/textView1" android:padding="5dp" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>