Android Material Design:popupmenu
The Android Material Design introduces a popupmenu similar to the context menu in the past, but more flexible. As shown in the figure:
Now give the code to implement the figure PopupMenu. This example is an ordinary button-triggered pop-up popupmenu.
Mainactivity.java of the test:
Package zhangphil.materialdesign;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v.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) {Supe
R.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) {Show
PopupMenu (button);
}
}); } private void ShowPopupMenu (view view) {//view the position of the relative View displayed by the current popupmenu 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
ApplicationContext (), 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 {@Override protected void onCreate (Bundle savedinstancestate) {Super.oncre
Ate (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 the position of the relative View displayed by the current popupmenu 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 bool EAN 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 P
Opupmenu menu) {Toast.maketext (Getapplicationcontext (), "Close PopupMenu", Toast.length_short). Show ();
}
});
Popupmenu.show (); }
}
The Activity_main layout file that Mainactivity.java needs contains only one ordinary Android Button, which is not included in the time.
Main.xml in the menu directory:
<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= "
app:showasaction=" never/>
<item
android:id= "@+id/action_new"
android:orderincategory= "
android:title=" new
app: showasaction= "Never"/>
<item
android:id=
"@+id/action_del" android:title= "Delete"
app:showasaction= "never"/>
The above content for you to share on the Android implementation click on a button to let the menu options from the button around the designated location pop-up knowledge, I hope you like.