Examples of PopupMenu

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.