如何更改android溢出按钮上弹出菜单的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23516247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:13:17  来源:igfitidea点击:

How change position of popup menu on android overflow button?

androidpopupandroid-actionbarandroid-menu

提问by user3266062

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of actionbar not under that. If you notice on pop menu of gmail overflow you saw that popmenu take place at edge of actionbar.

我只是喜欢在 Gmail 应用程序中实现与弹出菜单相同的东西,锚定到右上角的溢出按钮。为此,我使用了与 android Android 弹出菜单的google 教程相同的代码,但对我来说,在操作栏的边缘显示弹出菜单而不是在它之下。如果您注意到 gmail 溢出的弹出菜单,您会看到弹出菜单发生在操作栏的边缘。

This is the xml that I used for popup menu:

这是我用于弹出菜单的 xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"
        android:title="lablab"/>
    <item
        android:id="@+id/item2"
        android:title="lablab"/>

</menu>

and at the follow is in my activity:

以下是我的活动:

public void showFontSetting(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
        // TODO Auto-generated method stub

            switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(Index.this,
                        "You Clicked : " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item2:
                    break;
            }
            return true;
        }
    });
}

采纳答案by SuN

Add the following piece of code to your activity:

将以下代码添加到您的活动中:

PopupWindow popupwindow_obj; // create object

popupwindow_obj=popupDisplay();  // initialize in onCreate()

popupwindow_obj.showAsDropDown(clickbtn,-40, 18); // where u want show on view click event

public PopupWindow popupDisplay() { // disply designing your popoup window
    final PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view

    View view; 

    LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    view = inflater.inflate(R.layout.mylayout, null);

    Button item = (LinearLayout) view.findViewById(R.id.button1);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}

Create an XML in res/layoutdirectory and name it mylayout.xml

res/layout目录中创建一个 XML并命名mylayout.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
</LinearLayout>

回答by user1185087

To overlap only, use this approach:

要仅重叠,请使用以下方法:

PopupMenu popupMenu = new PopupMenu(getContext(), this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);

To get a PopupMenu with a bright background and a detailed control over the offsets use this approach:

要获得具有明亮背景和对偏移量的详细控制的 PopupMenu,请使用以下方法:

styles.xml

样式文件

<style name="PopupMenuOverlapAnchor" parent="@style/Theme.AppCompat.Light">
   <item name="android:overlapAnchor">true</item>
   <item name="android:dropDownVerticalOffset">0dp</item>
   <item name="android:dropDownHorizontalOffset">0dp</item>
</style>

Code:

代码:

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);

Does not work anymore:Here a simple way to adjust the position of a PopupMenu. It positions the menu over its anchor view (overflowButton) like the menu in the action bar:

不再起作用:这里有一个简单的方法来调整 a 的位置PopupMenu。它将菜单定位在其锚点视图 ( overflowButton) 上,就像操作栏中的菜单一样:

    PopupMenu popupMenu = new PopupMenu(context, overflowMenuButton);
    popupMenu.inflate(R.menu.my_menu);

    // Fix vertical offset
    overflowMenuButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            popupMenu.show();
            if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener)
            {
                ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu.getDragToOpenListener();
                listener.getPopup().setVerticalOffset(-overflowMenuButton.getHeight());
                listener.getPopup().show();
            }
        }
    });
    PopupMenu popupMenu = new PopupMenu(context, overflowMenuButton);
    popupMenu.inflate(R.menu.my_menu);

    // Fix vertical offset
    overflowMenuButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            popupMenu.show();
            if (popupMenu.getDragToOpenListener() instanceof ListPopupWindow.ForwardingListener)
            {
                ListPopupWindow.ForwardingListener listener = (ListPopupWindow.ForwardingListener) popupMenu.getDragToOpenListener();
                listener.getPopup().setVerticalOffset(-overflowMenuButton.getHeight());
                listener.getPopup().show();
            }
        }
    });

回答by Neeraj

Applying gravity helped in my case

在我的情况下应用重力有帮助

PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);

回答by Stephen Liu

After trying out each approach that I have found, I would think putting an anchoring view might still be an easier and simpler way, especially when you are using a more flexible layout, e.g. ConstraintLayout.

在尝试了我发现的每一种方法之后,我认为放置锚定视图可能仍然是一种更容易和更简单的方法,尤其是当您使用更灵活的布局时,例如 ConstraintLayout。

Just put an invisible view to where you want the popup menu to anchor:

只需在您希望弹出菜单锚定的位置放置一个不可见的视图:

<View
    android:id="@+id/anchor_menu"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toStartOf="@+id/button_menu"
    app:layout_constraintTop_toBottomOf="@+id/button_menu"
    />

Then use it as the anchoring view instead:

然后将其用作锚定视图:

    mPopupMenu = new PopupMenu(getActivity(), mPopupMenuAnchor);

Boom, it is done.

轰,大功告成。

回答by Sanket Kachhela

Maybe you might consider using PopupWindowinstead.

也许您可以考虑使用PopupWindow代替。

popupwindow.showAsDropDown(view,x,y);

Here xand yis position of popup window relative to your view.

这里xy是弹出窗口相对于您的视图的位置