Popup Menu at bottom of a view

时光怂恿深爱的人放手 提交于 2021-01-28 20:44:04

问题


How to show Popup menu always at bottom of the anchor view. This is the code I'm using to display popup menu.

 PopupMenu popup = new PopupMenu(activityReference, view, Gravity.NO_GRAVITY);
                popup.getMenuInflater()
                    .inflate(R.menu.popup_menu_event_edit, popup.getMenu());
popup.show();

I have tried changinging the Gravity.NO_GRAVITY to Gravity.BOTTOM. But its not working.


回答1:


It worked for me

PopupMenu attachFilePopup = new PopupMenu(this, view ,Gravity.BOTTOM);
attachFilePopup.inflate(R.menu.attachment_choices);

view => the bottom button where you want to show the menu

attachment_choices.xml

<item
    android:id="@+id/attach_location"
    android:title="@string/send_location"/>

<item
    android:id="@+id/attach_record_voice"
    android:title="@string/attach_record_voice"/>

<item
    android:id="@+id/attach_take_picture"
    android:title="@string/attach_take_picture"/>

<item
    android:id="@+id/attach_choose_picture"
    android:title="@string/attach_choose_picture"/>

<item
    android:id="@+id/attach_choose_file"
    android:title="@string/choose_file"/>




回答2:


here button1 is anchor view means referrence where you want to show your menu

PopupMenu popupMenu = new PopupMenu(context, button1, Gravity.TOP);
popupMenu.getMenuInflater().inflate(R.menu.home_drawer_bottom_nav_menu, popupMenu.getMenu());
popupMenu.show();



回答3:


Use this it is working for me.

popupMenu = new PopupMenu(MainActivity.this, anchorView);
popupMenu.inflate(R.menu.popup_menu);
            popupMenu.show();


来源:https://stackoverflow.com/questions/30644030/popup-menu-at-bottom-of-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!