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

4
我应该如何制作一个自定义菜单项,就像附图中菜单的第一行一样(Google Chrome应用程序的屏幕截图)?
我尝试在onCreateOptionsMenu中使用下面的代码,其中R.layout.menu_top_row是第一行的布局。但是在我的情况下只显示了一个空白行? 我能够显示其余选项,但无法显示第一行。
View child = getLayoutInflater().inflate(R.layout.menu_top_row, null);
    MenuItem menuItem=menu.add("");
    menuItem.setActionView(child);
1个回答

4

这个stack overflow答案中提到:

PopupMenu 用于显示菜单,但是没有很好的方法来自定义菜单项的外观。如果您想要更灵活的东西,那么您的答案是ListPopupWindow

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

这难道不仅允许相同类型的行(图标+文本)吗?如何使用它来显示不同的行(例如问题中显示的菜单中的第一行),该行具有多个图标? - Bootstrapper
您可以使用自己的自定义适配器,然后根据视图的位置,充气不同的布局。 - Andrew Orobator
我们如何在单击溢出菜单按钮时显示弹出窗口?感谢您在https://dev59.com/yojca4cB1Zd3GeqPv2Po?lq=1上提供任何帮助。 - source.rar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接