Android中使用gridview如何让图片在上文字在下

你说的是这样子么?
如果是这样子的话就看源码吧:
gridview.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:numColumns="auto_fit"  
  7.     android:verticalSpacing="10dp"  
  8.     android:horizontalSpacing="10dp"  
  9.     android:columnWidth="90dp"  
  10.     android:stretchMode="columnWidth"  
  11.     android:gravity="center"    
  12.     />  

item.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_width="fill_parent"  
  5.     android:paddingBottom="4dip"  
  6.     >  
  7.     <ImageView  
  8.         android:id="@+id/ItemImage"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         />  
  13.     <TextView  
  14.         android:id="@+id/ItemText"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_below="@+id/ItemImage"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_centerHorizontal="true"  
  19.         android:text="TextView01"  
  20.         />  
  21. </RelativeLayout>  

activity
Java代码 复制代码 收藏代码
  1. package com.test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5.   
  6. import android.app.Activity;   
  7. import android.os.Bundle;   
  8. import android.view.View;   
  9. import android.widget.AdapterView;   
  10. import android.widget.AdapterView.OnItemClickListener;   
  11. import android.widget.GridView;   
  12. import android.widget.SimpleAdapter;   
  13.   
  14. public class TestGridView extends Activity {   
  15.     private GridView gridview;   
  16.   
  17.     public void onCreate(Bundle savedInstanceState) {   
  18.         super.onCreate(savedInstanceState);   
  19.         setContentView(R.layout.gridview);   
  20.            
  21.         gridview = (GridView) findViewById(R.id.gridview);   
  22.   
  23.         // 生成动态数组,并且转入数据   
  24.         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();   
  25.         for (int i = 0; i < 10; i++) {   
  26.             HashMap<String, Object> map = new HashMap<String, Object>();   
  27.             map.put("ItemImage", R.drawable.icon);// 添加图像资源的ID   
  28.             map.put("ItemText""NO." + String.valueOf(i));// 按序号做ItemText   
  29.             lstImageItem.add(map);   
  30.         }   
  31.         // 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应   
  32.         SimpleAdapter saImageItems = new SimpleAdapter(this// 没什么解释   
  33.                 lstImageItem,// 数据来源   
  34.                 R.layout.item,// night_item的XML实现   
  35.                 // 动态数组与ImageItem对应的子项   
  36.                 new String[] { "ItemImage""ItemText" },   
  37.                 // ImageItem的XML文件里面的一个ImageView,两个TextView ID   
  38.                 new int[] { R.id.ItemImage, R.id.ItemText });   
  39.         // 添加并且显示   
  40.         gridview.setAdapter(saImageItems);   
  41.         // 添加消息处理   
  42.         gridview.setOnItemClickListener(new ItemClickListener());   
  43.     }   
  44.   
  45.     // 当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件   
  46.     class ItemClickListener implements OnItemClickListener {   
  47.         public void onItemClick(AdapterView<?> arg0,// The AdapterView where the click happened   
  48.                 View arg1,// The view within the AdapterView that was clicked   
  49.                 int arg2,// The position of the view in the adapter   
  50.                 long arg3// The row id of the item that was clicked   
  51.         ) {   
  52.             // 在本例中arg2=arg3   
  53.             @SuppressWarnings("unchecked")   
  54.             HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);   
  55.             // 显示所选Item的ItemText   
  56.             setTitle((String) item.get("ItemText"));   
  57.         }   
  58.     }   
  59. }  
posted @ 2012-03-09 16:28  zziss  阅读(892)  评论(0编辑  收藏  举报