简体   繁体   English

如何设置弹出菜单的位置?

[英]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.我正在使用 PopupMenu ,我必须在我单击的按钮下方修复弹出菜单的位置,但它显示在上方我的按钮下方是我所做的代码。

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 PopupMenu将像ActionBarhttps : //stackoverflow.com/a/29702608/1185087

Instead of popupMenu.show();而不是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).计算要显示弹出窗口的锚视图或按钮的位置以设置 Horizo​​ntalOffset(x) 和 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.如果您想根据需要定位菜单,请为此目的更好地使用对话框或弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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