Custom Menu item in Overflow menu

前端 未结 1 1184
天命终不由人
天命终不由人 2021-01-06 09:23

How should i make a Custom MenuItem, exactly like the first row in the menu of the attached picture ( screenshot of Google Chrome app)

相关标签:
1条回答
  • 2021-01-06 09:29

    Taken from this stack overflow answer:

    A PopupMenu is meant for displaying Menus and there really isn't a good way of customizing the appearance of the menu items. If you want something more flexible, your answer is 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();
    }
    
    0 讨论(0)
提交回复
热议问题