歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android控件GridView學習筆記

Android控件GridView學習筆記

日期:2017/3/1 11:13:15   编辑:Linux編程

GridView最常用的就是用來顯示九宮格這類似的。比如下面這個圖:



像這種,上面一個圖片,下面一段文字,這些是非常常見的。實現方法如下:

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout Android:id="@+id/widget33"
  3. android:layout_width="fill_parent" android:layout_height="wrap_content"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:paddingBottom="5dip">
  6. <ImageView
  7. android:id="@+id/item_image"
  8. android:layout_centerHorizontal="true"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"/>
  11. <TextView
  12. android:id="@+id/item_text"
  13. android:layout_below="@id/item_image"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_centerHorizontal="true"/>
  17. </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. map = new HashMap<String, Object>();
  15. map.put("itemImage", R.drawable.menu_search);
  16. map.put("itemText", "查找");
  17. data.add(map);
  18. map = new HashMap<String, Object>();
  19. map.put("itemImage", R.drawable.menu_delete);
  20. map.put("itemText", "刪除");
  21. data.add(map);
  22. map = new HashMap<String, Object>();
  23. map.put("itemImage", R.drawable.controlbar_showtype_list);
  24. map.put("itemText", "菜單");
  25. data.add(map);
  26. map = new HashMap<String, Object>();
  27. map.put("itemImage", R.drawable.menu_exit);
  28. map.put("itemText", "退出");
  29. data.add(map);
  30. SimpleAdapter adapter = new SimpleAdapter(this, data,
  31. R.layout.itemmenu, new String[]
  32. { "itemImage", "itemText" }, new int[]
  33. { R.id.item_image, R.id.item_text });
  34. gv_buttom_menu.setAdapter(adapter);
  1. //GridView上的item監聽
  1. gv_buttom_menu.setOnItemClickListener(new OnItemClickListener()
  2. {
  3. @Override
  4. public void onItemClick(AdapterView<?> parent, View view,
  5. int position, long id)
  6. {
  7. switch (position)
  8. {
  9. case 0:
  10. {
  11. startActivity(new Intent(StudentManagerActivity.this,
  12. AddStudentActivity.class));
  13. break;
  14. }
  15. case 1:
  16. {
  17. break;
  18. }
  19. case 2:
  20. {
  21. break;
  22. }
  23. case 3:
  24. {
  25. break;
  26. }
  27. case 4:
  28. {
  29. finish();
  30. break;
  31. }
  32. default:
  33. break;
  34. }
  35. }
  36. });
  37. }
Copyright © Linux教程網 All Rights Reserved