Android自定义菜单栏

1.以PopupWindow作为菜单容器

2.以GridView显示子菜单

3.重写menu按键事件

 

封装PopupWindow至ViewUtils

 

public class ViewUtils
{
	/*
	 *自定义菜单
	 *@param activity 显示菜单的activity
	 *@param menu_image_array 菜单每个item的图标
	 *@param menu_name_array 菜单每个item的名称
	 */
	public  PopupWindow createMenu(Activity activity,int[] menu_image_array,String[] menu_name_array)
	{
		View view = activity.getLayoutInflater().inflate(R.layout.view_menu, null); //自定义菜单的布局文件
		PopupWindow pop = new PopupWindow(view,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		pop.setBackgroundDrawable(new BitmapDrawable());
		pop.setOutsideTouchable(false);
		pop.setFocusable(true);
		
		//初始化GridView(子菜单)
		GridView menuGrid = (GridView) view.findViewById(R.id.menuGrid);
		menuGrid.setAdapter(getMenuAdapter(activity,menu_name_array, menu_image_array));
		return pop;
	}
	/*
	 * 自定义菜单GridView适配器
	 */
	private SimpleAdapter getMenuAdapter(Activity activity , String[] menuNameArray , int[] menuImageArray)
	{
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<menuNameArray.length;i++)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", menuImageArray[i]);
			map.put("itemName", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(activity, data, R.layout.item_menu, new String[]{"itemImage","itemName"},new int[]{R.id.menuItemImage,R.id.menuItemName});
		return adapter;
	}
}

在任何一个Activity,当需要显示菜单时:

ViewUtils viewUtils = new ViewUtils();
PopupWindow pop = viewUtils.createMenu(MainActivity.this,menu_image_array,menu_name_array);
pop.showAtLocation(getLayoutInflater().inflate(R.layout.XXX, null), Gravity.BOTTOM, 0, 0);


使用的布局文件


R.layout.view_menu

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >  
<GridView 
    android:id="@+id/menuGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/menu_background"
    android:listSelector="@drawable/menu_item_bg"
    android:numColumns="4"
    android:horizontalSpacing="10dip"
    android:verticalSpacing="10dip"/>
</LinearLayout>


R.layout.item_menu

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dip">  
<ImageView 
    android:id="@+id/menuItemImage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
<TextView
    android:id="@+id/menuItemName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:layout_below="@id/menuItemImage"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:textColor="@drawable/menu_item_text"
    android:textStyle="bold"/>"
</RelativeLayout>


重写menu按键事件

public boolean onCreateOptionsMenu(Menu menu) 
{
	ViewUtils viewUtils = new ViewUtils();
	PopupWindow pop = viewUtils.createMenu(MainActivity.this,menu_image_array,menu_name_array);
	pop.showAtLocation(getLayoutInflater().inflate(R.layout.XXX, null), Gravity.BOTTOM, 0, 0);
	return false;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值