Android控件GridView学习笔记


GridView最常用的就是用来显示九宫格这类似的。比如下面这个图:



像这种,上面一个图片,下面一段文字,这些是非常常见的。实现方法如下:

首先是GridView的一个Item的xml格式文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout Android:id="@+id/widget33"  
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  5.     xmlns:android="http://schemas.android.com/apk/res/android"  
  6.     android:paddingBottom="5dip">  
  7.       
  8.     <ImageView   
  9.         android:id="@+id/item_image"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"/>  
  13.           
  14.     <TextView   
  15.         android:id="@+id/item_text"  
  16.         android:layout_below="@id/item_image"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_centerHorizontal="true"/>  
  20. </RelativeLayout>  

然后在需要的地方写一个GridView

  1. <GridView   
  2.     android:id="@+id/gv_buttom_menu"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="65sp"   
  5.     android:layout_alignParentBottom="true">  
  6. </GridView>  

下面是实现方法:

  1. private void loadGridView()  
  2. {  
  3.     gv_buttom_menu = (GridView) findViewById(R.id.gv_buttom_menu);  
  4.     gv_buttom_menu.setBackgroundResource(R.drawable.channelgallery_bg);  
  5.     gv_buttom_menu.setNumColumns(5);  
  6.     gv_buttom_menu.setGravity(Gravity.CENTER);  
  7.     gv_buttom_menu.setHorizontalSpacing(10);  
  8.     gv_buttom_menu.setVerticalSpacing(10);  
  9.     ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  10.     HashMap<String, Object> map = new HashMap<String, Object>();  
  11.     map.put("itemImage", R.drawable.menu_new_user);  
  12.     map.put("itemText""增加");  
  13.     data.add(map);  
  14.   
  15.     map = new HashMap<String, Object>();  
  16.     map.put("itemImage", R.drawable.menu_search);  
  17.     map.put("itemText""查找");  
  18.     data.add(map);  
  19.   
  20.     map = new HashMap<String, Object>();  
  21.     map.put("itemImage", R.drawable.menu_delete);  
  22.     map.put("itemText""删除");  
  23.     data.add(map);  
  24.   
  25.     map = new HashMap<String, Object>();  
  26.     map.put("itemImage", R.drawable.controlbar_showtype_list);  
  27.     map.put("itemText""菜单");  
  28.     data.add(map);  
  29.   
  30.     map = new HashMap<String, Object>();  
  31.     map.put("itemImage", R.drawable.menu_exit);  
  32.     map.put("itemText""退出");  
  33.     data.add(map);  
  34.   
  35.     SimpleAdapter adapter = new SimpleAdapter(this, data,  
  36.             R.layout.itemmenu, new String[]  
  37.             { "itemImage""itemText" }, new int[]  
  38.             { R.id.item_image, R.id.item_text });  
  39.     gv_buttom_menu.setAdapter(adapter);  
  1. //GridView上的item监听  
  1.     gv_buttom_menu.setOnItemClickListener(new OnItemClickListener()  
  2.     {  
  3.   
  4.         @Override  
  5.         public void onItemClick(AdapterView<?> parent, View view,  
  6.                 int position, long id)  
  7.         {  
  8.             switch (position)  
  9.             {  
  10.             case 0:  
  11.             {  
  12.                 startActivity(new Intent(StudentManagerActivity.this,  
  13.                         AddStudentActivity.class));  
  14.                 break;  
  15.             }  
  16.             case 1:  
  17.             {  
  18.   
  19.                 break;  
  20.             }  
  21.             case 2:  
  22.             {  
  23.   
  24.                 break;  
  25.             }  
  26.             case 3:  
  27.             {  
  28.   
  29.                 break;  
  30.             }  
  31.             case 4:  
  32.             {  
  33.                 finish();  
  34.                 break;  
  35.             }  
  36.             default:  
  37.                 break;  
  38.             }  
  39.   
  40.         }  
  41.   
  42.     });  
  43. }  

相关内容