android控件学习——GridView学习

来源:互联网 发布:数据质量问题库设计 编辑:程序博客网 时间:2024/06/08 13:59

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(){@Overridepublic 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;}}});}



原创粉丝点击