Using PopupMenu in Android apps

Source: Internet
Author: User

Final effect:

Page layout:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 Android:background= "#FFFFFF"6 android:orientation= "vertical" >7 8     <Relativelayout9         Android:layout_width= "Fill_parent"Ten Android:layout_height= "45DP" One Android:background= "#303538" > A         <Button -             Android:id= "@+id/button_showpopup" - Android:layout_width= "Wrap_content" the Android:layout_height= "Fill_parent" - Android:layout_alignparentright= "true" - Android:background= "#303538" - Android:text= "┇" + Android:textcolor= "#FFFFFF" - android:textsize= "20SP" > +         </Button> A     </Relativelayout> at  - </LinearLayout>
popupmenu Layout (file in menu directory):
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >3 4     <Item5         Android:id= "@+id/about"6 android:orderincategory= "+"7 android:showasaction= "Never"8 Android:title= "about">9     </Item>Ten     <Item One         Android:id= "@+id/exit" A android:orderincategory= "+" - android:showasaction= "Never" - Android:title= "Exit"> the     </Item> -  - </Menu>
Background code:
1  PackageCom.wangzhen.popup;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.MenuItem;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.view.Window;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.PopupMenu; One ImportAndroid.widget.Toast; A  -  Public classMainextendsActivityImplementsOnclicklistener, - Android.widget.PopupMenu.OnMenuItemClickListener { the     /**Called when the activity is first created.*/ -     PrivateButton Button_showpopup; -  - @Override +      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + requestwindowfeature (window.feature_no_title); A Setcontentview (r.layout.main); at initviews (); -     } -  -     //Initializing the control -     Private voidinitviews () { -         //TODO auto-generated Method Stub inButton_showpopup =(Button) Findviewbyid (r.id.button_showpopup); -Button_showpopup.setonclicklistener ( This); to     } +  -      Public voidOnClick (View arg0) { the         //TODO auto-generated Method Stub *         Switch(Arg0.getid ()) { $          CaseR.id.button_showpopup:Panax NotoginsengPopupMenu pop =NewPopupMenu ( This, arg0); - pop.getmenuinflater (). Inflate (R.menu.menu, Pop.getmenu ()); the pop.show (); +Pop.setonmenuitemclicklistener ( This); A              Break; the         } +     } -  $      Public BooleanOnmenuitemclick (MenuItem arg0) { $         //TODO auto-generated Method Stub -         Switch(Arg0.getitemid ()) { -          Caser.id.about: theToast.maketext ( This, "about", 1000). Show (); -              Break;Wuyi          CaseR.id.exit: the finish (); -              Break; Wu         } -         return false; About     } $}

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.