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;
}