android:horizontalSpacing 元素之间的水平间距
android:verticalSpacing 元素之间的垂直间距
android:numColumns 设置列数
android:stretchMode 拉伸模式
该程序的目的是将图片用适配器放入4列的gridView中,点击一个图片下方出现预览图。
package com.kale.gridview02; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ImageView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { GridView gV; ImageView iV; //将图片Id放入数组中去 int []imageIds = new int[] { R.drawable.itunes,R.drawable.appstore, R.drawable.calculator,R.drawable.camera,R.drawable.mail, R.drawable.setting,R.drawable.safair }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewInit(); //定义一个list来存放多个item元素 ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>(); for (int i = 0; i < imageIds.length; i++) { Map<String, Object> item = new HashMap<>(); item.put("image", imageIds[i]); listItems.add(item); } //创建simpleAdapter来设置元素。1.context对象 2.list对象(数据源)3.item的布局文件 4.元素中的键名 5.该键名对应的组件的id SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.item, new String[] {"image"}, new int[] {R.id.item_imageView_id}); gV.setAdapter(adapter); gV.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { //点击哪个图片,下面的imageview就显示哪个 iV.setImageResource(imageIds[position]); } }); } private void viewInit() { gV = (GridView)findViewById(R.id.gridView_id); iV = (ImageView)findViewById(R.id.imageView_id); } }
<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" tools:context="${relativePackage}.${activityClass}" > <!-- android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing 元素之间的垂直间距 android:numColumns 设置列数 android:stretchMode 拉伸模式 --> <GridView android:id="@+id/gridView_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:horizontalSpacing="1dp" android:verticalSpacing="1dp" android:numColumns="4" android:gravity="center"> </GridView> <ImageView android:id="@+id/imageView_id" android:layout_width="180dp" android:layout_height="180dp" android:layout_below="@+id/gridView_id" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/imageView_id" android:layout_marginLeft="23dp" android:textSize="20sp" android:text="预览图片" /> </RelativeLayout>
item.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/item_imageView_id" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="fitXY" android:src="@drawable/ic_launcher" /> </LinearLayout>