问题
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