首先看下效果图:
准备工作:在要使用gridview的xml中加入gridview控件:
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:cacheColorHint="@color/transparent"
android:gravity="center"
android:listSelector="@color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>
新建一个grid_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/selector_dianji"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/person_imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:contentDescription="@null"/>
<TextView
android:id="@+id/person_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@drawable/bangniz_icon_fengexian"></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@drawable/bangniz_icon_fengexian"></LinearLayout>
</LinearLayout>
在要使用的activity中加入以下代码:
//初始化gridview数据,此方法在oncreate()方法中调用
ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();//此句代码放在activity的最上方
private void initializeManagerItems() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.znj_grzx_tb);
map.put("ItemText", "我的余额");
dataList.add(map);
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("ItemImage", R.drawable.znj_grzx_tb6);
map1.put("ItemText", "关于我们");
dataList.add(map1);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("ItemImage", R.drawable.znj_grzx_tb3);
map2.put("ItemText", "我的红包");
dataList.add(map2);
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("ItemImage", R.drawable.znj_grzx_tb4);
map3.put("ItemText", "我的活动");
dataList.add(map3);
HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("ItemImage", R.drawable.znj_grzx_tb11);
map4.put("ItemText", "经纪人认证");
dataList.add(map4);
HashMap<String, Object> map5 = new HashMap<String, Object>();
map5.put("ItemImage", R.drawable.znj_grzx_tb12);
map5.put("ItemText", "我的客户");
dataList.add(map5);
HashMap<String, Object> map6 = new HashMap<String, Object>();
map6.put("ItemImage", R.drawable.znj_grzx_tb13);
map6.put("ItemText", "订单管理");
dataList.add(map6);
HashMap<String, Object> map7 = new HashMap<String, Object>();
map7.put("ItemImage", R.drawable.znj_grzx_yyz);
map7.put("ItemText", "我的邀友");
dataList.add(map7);
HashMap<String, Object> map8 = new HashMap<String, Object>();
map8.put("ItemImage", R.drawable.znj_grzx_myrecord);
map8.put("ItemText", "我的账单");
dataList.add(map8);
GridView gridView = (GridView) findViewById(R.id.gridView1);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, dataList,// 数据源
R.layout.grid_view_item,// 显示布局
new String[] { "ItemImage", "ItemText" }, new int[] {
R.id.person_imageView, R.id.person_textView });
gridView.setAdapter(simpleAdapter);
gridView.setOnItemClickListener(new ItemClickListener());
}
//点击选项代码(根据position来对应选择,position为datalist的下标):
class ItemClickListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position,
long rowid) {
switch (position) {
case 0:
// Intent intent0 = new Intent(PersonManagerActivity.this,
// fpWebViewActivity.class);
// intent0.putExtra("url", Constants.TALK_IMAGE_URL+"/page/hongbao/mybalance.html?userId="+user.uid);
// intent0.putExtra("title", "我的余额");
// startActivity(intent0);
break;
case 1:
// Intent intent1 = new Intent(PersonManagerActivity.this,
// fpWebViewActivity.class);
// intent1.putExtra("url", Constants.TALK_IMAGE_URL+"/page/login/aboutMe.html?userId="+user.uid);
// intent1.putExtra("title", "关于我们");
// startActivity(intent1);
break;
case 2:
// Intent intent2 = new Intent(PersonManagerActivity.this,
// MyRedPacketActivity.class);
// startActivity(intent2);
break;
case 3:
Intent intent3 = new Intent(PersonManagerActivity.this,MyActivity.class);
// Intent intent3 = new Intent(PersonManagerActivity.this,fpWebViewActivity.class);
// intent3.putExtra("url", Constants.TALK_IMAGE_URL+"/page/activity/myindex.html?userId="+user.uid);
// intent3.putExtra("title", "我的活动");
// startActivity(intent3);
break;
case 4:
// Intent intent4 = new Intent(PersonManagerActivity.this,
// ApplyecoActivity.class);
// intent4.putExtra("userId", user.uid);
// startActivity(intent4);
break;
case 5:
// Intent intent5 = new Intent(PersonManagerActivity.this,
// fpWebViewActivity.class);
// intent5.putExtra("url", Constants.TALK_IMAGE_URL+"/page/tjz/tjzwdkh.html?userId="+user.uid);
// intent5.putExtra("title", "我的客户");
// startActivity(intent5);
break;
case 6:
// Intent intent6 = new Intent(PersonManagerActivity.this,
// OrderManagerActivity.class);
// intent6.putExtra("userId", user.uid);
// startActivity(intent6);
break;
case 7:
// Intent intent7 = new Intent(PersonManagerActivity.this,
// MyFriendActivity.class);
//
// startActivity(intent7);
break;
case 8:
// Intent intent8 = new Intent(PersonManagerActivity.this,
// fpWebViewActivity.class);
// intent8.putExtra("url", Constants.TALK_IMAGE_URL+"/page/grzx/zhangdan.html?userId="+user.uid);
// intent8.putExtra("title", "我的账单");
// startActivity(intent8);
break;
default:
break;
}
}
}