Android 溢出菜单中的自定义菜单项

Android 溢出菜单中的自定义菜单项,android,android-menu,Android,Android Menu,我应该如何制作一个定制的菜单项,就像所附图片菜单中的第一行一样(谷歌Chrome应用程序截图) 我在oncreateoptions菜单中尝试了下面的代码,其中R.layout.menu\u top\u row是第一行的布局。但在我的例子中,只有一个空行出现?我可以显示其余选项,但无法显示第一行 View child = getLayoutInflater().inflate(R.layout.menu_top_row, null); MenuItem menuItem=menu.add(

我应该如何制作一个定制的菜单项,就像所附图片菜单中的第一行一样(谷歌Chrome应用程序截图)

我在
oncreateoptions菜单中尝试了下面的代码,其中
R.layout.menu\u top\u row
是第一行的布局。但在我的例子中,只有一个空行出现?我可以显示其余选项,但无法显示第一行

View child = getLayoutInflater().inflate(R.layout.menu_top_row, null);
    MenuItem menuItem=menu.add("");
    menuItem.setActionView(child);
:

A是用来显示的,实际上没有一个好的方法来定制菜单项的外观。如果你想要更灵活的东西,你的答案是

private静态最终字符串TITLE=“TITLE”;
私有静态最终字符串ICON=“ICON”;
私有列表数据=新的ArrayList();
//使用此选项可将项目添加到ListPopupWindow将使用的列表中
私有void附加项(字符串标题,int-iconResourceId){
HashMap=newHashMap();
地图放置(标题,标题);
map.put(图标,iconResourceId);
数据。添加(地图);
}
//如果要显示ListPopupWindow,请调用此选项
私有void showListMenu(查看锚定){
ListPopupWindow-popupWindow=新ListPopupWindow(此);
ListAdapter=新的SimpleAdapter(
这
数据,
android.R.layout.activity\u list\u item,//您可能需要使用自己的酷布局
新字符串[]{TITLE,ICON},//这些只是数据使用的键
new int[]{android.R.id.text1,android.R.id.icon});//要将数据映射到的视图id
popupWindow.setAnchorView(锚);
popupWindow.setAdapter(适配器);
setWidth(400);//注意:不要使用像素,使用dimen资源
popupWindow.setOnItemClickListener(myListener);//选择列表项时的回调
popupWindow.show();
}

这不是只允许相同类型的行(图标+文本)吗?如何使用它来显示具有多个图标的不同行(如问题中显示的菜单中的第一行)?您可以使用自己的自定义适配器,然后根据视图的位置,将不同的布局充气。如何在单击溢出菜单按钮时显示弹出窗口?我想请你帮我解答一下这个问题
private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    map.put(ICON, iconResourceId);
    data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
    ListPopupWindow popupWindow = new ListPopupWindow(this);

    ListAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[] {TITLE, ICON}, // These are just the keys that the data uses
            new int[] {android.R.id.text1, android.R.id.icon}); // The view ids to map the data to


    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
    popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
    popupWindow.show();
}