Custom PopupMenu (layout)

前端 未结 2 760
忘掉有多难
忘掉有多难 2020-12-03 08:29

I\'m trying to upgrade my PopupMenu so it would come with icons and custom styles.
I have created a new layout for it



        
2条回答
  •  情书的邮戳
    2020-12-03 09:05

    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> data = new ArrayList>();
    
    // Use this to add items to the list that the ListPopupWindow will use
    private void addItem(String title, int iconResourceId) {
        HashMap map = new HashMap();
        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();
    }
    

提交回复
热议问题