<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/layout" android:gravity="center"> <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchMode="columnWidth" android:numColumns="4"/> </LinearLayout>
<?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/image" android:paddingLeft="10px" android:scaleType="fitCenter" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:id="@+id/title"/> </LinearLayout>
package com.example.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.SimpleAdapter; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView=(GridView)findViewById(R.id.gridView1);//获取GridView组件 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.img_top,R.drawable.ic_launcher,R.drawable.in,R.drawable.stop,};//定义并初始化保存图片id的数组 String[] title=new String[]{"图片一","超级图片","魅力图片","碉堡图片","搞笑图片","动画图片","开心图片","难过图片","诡异图片", "心灵鸡汤","虎虎生威","洋洋得意"};//定义并初始化保存说明文字的数组 List<Map<String,Object>> listitems=new ArrayList<Map<String,Object>>();//创建一个list集合 //通过for循环将图片和列表项文字放到Map中,并添加到List集合中 for(int i=0;i<imageId.length;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("image", imageId[i]); map.put("title",title[i]); listitems.add(map); } //创建SimpleAdapter SimpleAdapter adapter=new SimpleAdapter(this, listitems,R.layout.item,new String[]{"title","image"},new int[]{R.id.title,R.id.image}); gridView.setAdapter(adapter);//将适配器与gridView关联 } }
显示效果如图所示。
可以看出,GridView的实现方式和ListView十分相像
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView=(GridView)findViewById(R.id.gridView1);//获取GridView组件 final 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.img_top,R.drawable.ic_launcher,R.drawable.in,R.drawable.stop,};//定义并初始化保存图片id的数组 BaseAdapter baseAdapter=new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { 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;//如果以前new过View就使用convertView的缓存 } imageView.setImageResource(imageId[position]);//为imageView设置要显示的图片 return imageView;//返回imageView } //功能:获得当前选项的id @Override public long getItemId(int position) { return position; } //功能:获得当前选项 @Override public Object getItem(int position) { return position; } //获得数量 @Override public int getCount() { return imageId.length; } }; gridView.setAdapter(baseAdapter);//将适配器与gridView关联 } }
实现效果如图所示:
转载请注明出处:http://blog.csdn.net/acmman/article/details/44942337