弹出菜单出现在左下角 [英] Popup menu comes out in left bottom

查看:55
本文介绍了弹出菜单出现在左下角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的片段中实现弹出菜单,但结果远远超出了我的愿望.我在操作栏上有一个按钮,其中一个按钮调用弹出菜单,直到这里都没问题.它成功调用,我可以看到弹出菜单.我就是这样做的.

I am trying to implement popup menu in my fragment but the result is far beyond my desire. I have a button on the action bar and one of these buttons call the popup menu and no problem till here. It successfully calls and I can see the popup menu. Here is how I do that.

在片段类中

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.x) {           
    } else showPopup(view /* or getView() */);

    return super.onOptionsItemSelected(item);
}

public void showPopup(View v) {
    PopupMenu popupMenu = new PopupMenu(getContext(), v);
    MenuInflater menuInflater = popupMenu.getMenuInflater();
    menuInflater.inflate(R.menu.event_popup, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return false;
        }
    });
    popupMenu.show();
}

popup_menu.xml

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/a"
    android:icon="@drawable/ic_action_back"
    android:title="a" />

<item android:id="@+id/b"
    android:icon="@drawable/ic_action_back"
    android:title="b" />

Fragment 的操作菜单(包括打开弹出菜单的按钮)

Fragment's action menu (includes button which opens popup menu)

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/x"
    android:icon="@drawable/ic_action_back"
    yourapp:showAsAction="ifRoom"
    android:title="" />

<item android:id="@+id/y"
    android:icon="@drawable/ic_action_menu_dot"
    yourapp:showAsAction="ifRoom"
    android:title="" />

</menu>

结果就是它的样子.我希望它在右上角.我该如何处理.据我所知,它的默认位置已经在右上角.

And in the result here is how it looks. I want it to be on top right. How can I handle this. As far as I know, Its default placement is already top right.

推荐答案

您需要为弹出菜单提供适当的锚点视图(在本例中为溢出菜单).

You need to provide proper anchor view (overflow menu in this case) to the popup menu.

尝试改变

showPopup(view);

View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);

这应该将弹出菜单带到顶部.如果它仍然在左侧使用

This should bring the popup menu to top. If its still on the left side use

popupMenu.setGravity(Gravity.END);

这篇关于弹出菜单出现在左下角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆