PopupMenu can be very convenient to display a pop-up menu below the specified view, similar to the effect of the Actionbar overflow menu. Its menu options are available from the menu resource and are therefore very convenient. Here is a picture of a popupmenu running on the internet.
How to use
New Popup_menu.xml under Res/menu
| 1234567891011 |
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <item android:id="@+id/special_topic" android:title="专题"/> <item android:id="@+id/elite" android:title="精选"/> </menu> |
Use PopupMenu in activity or fragment
| 1234567891011121314151617181920 |
final ImageButton moreMenu = (ImageButton)v.findViewById(R.id.more_menu); moreMenu.setOnClickListener(newOnClickListener() { @Override public void onClick(View v){ PopupMenu popup = newPopupMenu(getActivity(), moreMenu); //Inflating the Popup using xml file popup.getMenuInflater() .inflate(R.menu.poupup_menu_home, popup.getMenu()); //registering popup with OnMenuItemClickListener popup.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { return true; } }); popup.show(); //showing popup menu } }); |
Where Moremenu is a button that is triggered by his popupmenu display.
Use of PopupMenu in Android