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

3

我正在使用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();
    }
});

以下是我得到的输出结果:

这就是我得到的输出结果。

请帮助我。

弹出菜单是警告对话框吗? - Mukesh Kumar Singh
3个回答

1

不要使用popupMenu.show();,而是需要这样做

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

计算锚点视图或按钮的位置,以设置弹出窗口的水平偏移(x)和垂直偏移(y)。


1

0

弹出菜单始终锚定在一个视图(按钮)上,并且将始终出现在其附加的视图上方或下方,具体取决于可用空间。

如果您想更好地根据需要定位菜单,请使用对话框或弹出窗口。


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