A pop-up menu is a modal menu docked on a view. If there is space below the View object, the pop-up menu will appear below the docking object, otherwise it will appear above. This is very useful:
Source Address:http://download.csdn.net/detail/u013922681/8951537
article references:
1.Android Examples of PopupMenu
The location of the menu in the 2.Android app displays the problem
Source:
Mainactivity.java
PackageCom.example.popMenu;ImportAndroid.os.Bundle;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.PopupMenu;ImportAndroid.widget.PopupMenu.OnMenuItemClickListener;ImportAndroid.widget.Toast; Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); } Public void ShowPopup(View view) {PopupMenu PopupMenu =NewPopupMenu (mainactivity. This, view); Popupmenu.getmenuinflater (). Inflate (R.menu.main, Popupmenu.getmenu ()); Popupmenu.setonmenuitemclicklistener (NewOnmenuitemclicklistener () {@Override Public Boolean Onmenuitemclick(MenuItem Item) {//TODO auto-generated method stub Switch(Item.getitemid ()) { CaseR.id.action_settings:toast.maketext (mainactivity. This,"click",0). Show (); Break;default: Break; }return false; } }); Popupmenu.show (); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override Public Boolean onoptionsitemselected(MenuItem Item) {//TODO auto-generated method stub Switch(Item.getitemid ()) { CaseR.id.action_settings:toast.maketext (mainactivity. This,"click",0). Show (); Break;default: Break; }return Super. onoptionsitemselected (item); }}
Activity_main.xml
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context=". Mainactivity "> <button android:id="@+id/button1"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_alignparentleft="true"android:layout_alignparenttop="true"android:layout_marginleft="96DP"android:layout_margintop="111DP"android:text="@string/hello_world"android:onclick="ShowPopup"/></relativelayout>
Effect
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Examples of PopupMenu