android控件學習——GridView學習

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



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

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

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/widget33"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:paddingBottom="5dip">
	
	<ImageView 
		android:id="@+id/item_image"
		android:layout_centerHorizontal="true"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
		
	<TextView 
		android:id="@+id/item_text"
		android:layout_below="@id/item_image"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_centerHorizontal="true"/>
</RelativeLayout>

然後在需要的地方寫一個GridView

	<GridView 
		android:id="@+id/gv_buttom_menu"
		android:layout_width="fill_parent"
		android:layout_height="65sp" 
		android:layout_alignParentBottom="true">
	</GridView>


下面是實現方法:

	private void loadGridView()
	{
		gv_buttom_menu = (GridView) findViewById(R.id.gv_buttom_menu);
		gv_buttom_menu.setBackgroundResource(R.drawable.channelgallery_bg);
		gv_buttom_menu.setNumColumns(5);
		gv_buttom_menu.setGravity(Gravity.CENTER);
		gv_buttom_menu.setHorizontalSpacing(10);
		gv_buttom_menu.setVerticalSpacing(10);
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.menu_new_user);
		map.put("itemText", "增加");
		data.add(map);

		map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.menu_search);
		map.put("itemText", "查找");
		data.add(map);

		map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.menu_delete);
		map.put("itemText", "刪除");
		data.add(map);

		map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.controlbar_showtype_list);
		map.put("itemText", "菜單");
		data.add(map);

		map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.menu_exit);
		map.put("itemText", "退出");
		data.add(map);

		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.itemmenu, new String[]
				{ "itemImage", "itemText" }, new int[]
				{ R.id.item_image, R.id.item_text });
		gv_buttom_menu.setAdapter(adapter);

//GridView上的item監聽

		gv_buttom_menu.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				switch (position)
				{
				case 0:
				{
					startActivity(new Intent(StudentManagerActivity.this,
							AddStudentActivity.class));
					break;
				}
				case 1:
				{

					break;
				}
				case 2:
				{

					break;
				}
				case 3:
				{

					break;
				}
				case 4:
				{
					finish();
					break;
				}
				default:
					break;
				}

			}

		});
	}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章