Android Material Design:popupmenu
Android Material Design introduced popupmenu similar to the past context menu, but more flexible. :
Now give the code to implement PopupMenu. This example is a normal button triggering pop-up popupmenu.
Mainactivity.java of the test:
[Java]View Plaincopyprint?
- Package zhangphil.materialdesign;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.support.v7.widget.PopupMenu;
- Import Android.view.MenuItem;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.Toast;
- Public class Mainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Final Button button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- ShowPopupMenu (button);
- }
- });
- }
- private void ShowPopupMenu (view view) {
- //view current PopupMenu The location of the relative view displayed
- PopupMenu PopupMenu = new PopupMenu (this, view);
- //Menu Layout
- Popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ());
- //menu item Click event
- Popupmenu.setonmenuitemclicklistener (new Popupmenu.onmenuitemclicklistener () {
- @Override
- Public Boolean Onmenuitemclick (MenuItem item) {
- Toast.maketext (Getapplicationcontext (), Item.gettitle (), Toast.length_short). Show ();
- return false;
- }
- });
- //PopupMenu Shutdown Event
- Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {
- @Override
- public void Ondismiss (PopupMenu menu) {
- Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();
- }
- });
- Popupmenu.show ();
- }
- }
Package Zhangphil.materialdesign;import Android.app.activity;import Android.os.bundle;import Android.support.v7.widget.popupmenu;import Android.view.menuitem;import Android.view.view;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Final button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {showpopupmenu (button);}}); private void ShowPopupMenu (view view) {///view current PopupMenu Displays the location of the relative View popupmenu popupmenu = new PopupMenu (this, View);//M ENU layout popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ());// menu item Click event Popupmenu.setonmenuitemclicklistener (New Popupmenu.onmenuitemclicklistener () {@Overridepublic Boolean Onmenuitemclick (MenuItem item) {Toast.maketext (Getapplicationcontext (), Item.gettitle (), toAst. Length_short). Show (); return false;}); /PopupMenu Shutdown Event Popupmenu.setondismisslistener (new Popupmenu.ondismisslistener () {@Overridepublic void Ondismiss ( PopupMenu menu) {Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();}); Popupmenu.show ();}}
The Activity_main layout file required by Mainactivity.java contains only one normal Android Button, which is not mentioned again.
Main.xml in the menu directory:
[HTML]View Plaincopyprint?
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="Http://schemas.android.com/apk/res-auto"
- xmlns:tools="Http://schemas.android.com/tools" >
- <item
- android:id="@+id/action_open"
- android:orderincategory="
- android:title="open"
- app:showasaction="never"/>
- <item
- android:id="@+id/action_new"
- android:orderincategory="
- android:title="new"
- app:showasaction="never"/>
- <item
- android:id="@+id/action_del"
- android:orderincategory="
- android:title="Delete"
- app:showasaction="never"/>
- </Menu>
Click a button to have the menu option pop-up from the specified position around the button.