简体   繁体   中英

How to set the position of Pop Up Menu?

I am using the PopupMenu ,i have to fix the position of the pop up menu below the buttton which i click,but it show on the above the my button below is the code which i do.

private final static int ONE = 1;
private final static int TWO = 2;
private final static int THREE = 3;

PopupMenu popupMenu = new PopupMenu(context, convertView.findViewById(R.id.txtOverflowIconList_item_Egov));

popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Item 1");
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "Item 2");
// popupMenu.getMenu().add(Menu.NONE, THREE, Menu.NONE, "Item 3");

popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {

        switch (item.getItemId()) {
            case ONE:
                Toast.makeText(context, "first ", 100).show();
                break;
            case TWO:
                Toast.makeText(context, "Two ", 100).show();
                break;
        }
        return false;
    }
});


holder.txtOverflowIcon.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        popupMenu.show();
    }
});

Below is th output what i get:

这是我得到的输出。

please help me.

I could fix that problem. The PopupMenu will reveal like the one in the ActionBar : https://stackoverflow.com/a/29702608/1185087

Instead of popupMenu.show(); You need to do like this

        popupMenu.show();
        if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener) {
            ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu
                    .getDragToOpenListener();
            listener.getPopup().setHorizontalOffset(x);
            listener.getPopup().setVerticalOffset(y);

            listener.getPopup().show();
        }

Calculate the position of anchor view or Button you want to display the popup to set the HorizontalOffset(x) and VerticalOffset(y).

pop up menu is always anchored to a view (button) and will always appear above or below the view it is attached to, depending upon the space available.

if you want to position the menu as your needs better use dialogs for this purpose or pop up window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM