Simple implementation of Android pop-up menu effect _android

Source: Internet
Author: User

This example for you to share the Android pop-up menu effect of the specific code for your reference, the specific content as follows

function Description: The user clicks the button pop-up menu. When the user selects a menu item, the Menuitemclick event is triggered and the pop-up menu disappears; If the user clicks outside the menu, the pop-up menu disappears directly. When the menu disappears, a dismissevent event is raised (this event can be used to do some post-processing when the menu disappears).

1. Operation effect

2. Add menu items
Add a menu subfolder under the Resources folder, and then add a file named Demo07_popup_menu.xml under this subfolder:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <item android:id=" @+id/item1 "
     android:title=" Item 1,/> <item android:id=  
  "@+id/ Item1 "
     android:title=" Item 2/> 
  <item android:id= "@+id/item1"
     android:title= "Item 3"/>
</menu> 

3, add Demo07_popupmenu.axml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent" >
  <button
    android:id= "@+id/popupbutton" android:layout_width= "Fill_"
    Parent "
    android:layout_height=" wrap_content "
    android:text=" @string/showpopup "/>
</ Linearlayout> 

Save all files.

4, add Demo07PopupMenu.cs

 using Android.app; using Android.os; using Android.widget; Namespace Ch05demos.  srcactivity {[Activity (Label = ' demo07popupmenu ')] public class Demo07popupmenu:activity {protected override void OnCreate (Bundle savedinstancestate) {base.
      OnCreate (savedinstancestate);

      Setcontentview (Resource.Layout.demo07_Popup);
      Button btn = findviewbyid<button> (Resource.Id.popupButton); Btn.
        Click + = (s, arg) => {popupmenu menu = new PopupMenu (this, BTN); Menu.
        Inflate (Resource.Menu.demo07_popup_menu); Menu. Menuitemclick = = (sender, args) => {string str = string. Format ("you selected: {0}", args.)
          Item); Toast.maketext (This,str, Toastlength.short).
        Show ();
        }; Menu.
        Dismissevent + = (sender, args) => {//menu disappears to do some post-processing in this event}; Menu.
      Show ();
    }; }
  }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.