Android Imitation micro-letter Menu

Source: Internet
Author: User
Tags bool

This article is to use the Android menu to implement, of course, you can use XML to do the same function, basic Java and C # Writing are consistent, so the use of XML in this article using Java Demo, it is necessary to note that, if not vs development, The resource file name must begin with lowercase or an error will be made.

Run effect

C # implementation

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64-65 Using Android.app; Using Android.os; Using Android.views; Using Android.widget;   namespace Menudemo {[Activity (Label = "@string/applicationname", Mainlauncher = true, Icon = "@drawable/icon")] Pu Blic class Mainactivity:activity {protected override void OnCreate (Bundle Bundle) {base. OnCreate (bundle);   Setcontentview (Resource.Layout.Main); var IV = findviewbyid<imageview> (RESOURCE.ID.IV); iv. Setimageresource (Resource.Drawable.test); Registerforcontextmenu (iv); ///associated with an activity public override bool Oncreateoptionsmenu (Imenu menu) {base. Oncreateoptionsmenu (menu); const int menuitemid = Menu.first; const int menuitemorder = Menu.none; const int munuitemtext = RESOURCE.STRING.MENUITEM1; * * First parameter: Group value * Second parameter: Menu unique indicator * Third parameter: Sequential parameter * Fourth parameter: text/menu. ADD (1, Menuitemid, Menuitemorder, Munuitemtext); Menu. ADD (2, Menuitemid + ten, Menuitemorder + 1, "Menu 2"); Menu. ADD (2, Menuitemid +, Menuitemorder + 2, "menu 3"); var submenu = menu. Addsubmenu (0, Menuitemid +, Menuitemorder + 3, "Sub-menu"); Submenu.setheadericon (Resource.Drawable.more); var subMenuItem1 = submenu.add (0, Menuitemid +, Menuitemorder + 4, "submenu-1"); Submenuitem1.seticon (Resource.Drawable.more); var subMenuItem2 = submenu.add (0, Menuitemid +, Menuitemorder + 5, "submenu-2"). Setcheckable (TRUE); Submenuitem2.seticon (Resource.Drawable.blue); Submenu.add (0, Menuitemid +, Menuitemorder + 6, "submenu-3"). Setcheckable (TRUE); return true; }   public override bool Onoptionsitemselected (Imenuitem Item) {Toast.maketext (this, "current menu number" + item.) ItemId, Toastlength.long). Show (); Return base. onoptionsitemselected (item); }     public override void Oncreatecontextmenu (IContextMenu menu, View V, Icontextmenucontextmenuinfo menuinfo) {base. Oncreatecontextmenu (menu, V, menuinfo); Menu. Setheadertitle ("context menu"); Menu. Setheadericon (Resource.Drawable.blue); Menu. ADD (0, Menu.first, Menu.none, "menu 1"); Menu. ADD (0, Menu.first+1,menu.none, "Menu 2"). Setshortcut (' 2 ', ' B '); Menu. ADD (0, Menu.first + 2, Menu.none, "Menu 3"); var submenu = menu. Addsubmenu ("submenu"); Submenu.add ("submenu Options"); } } }

Layout

?
1 2 3 4 5 6 7 8 9 10 11-12 <?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 " Android:minwidth= "25px" android:minheight= "25px" > <imageview android:layout_width= "fill_parent" Android: layout_height= "Wrap_content" android:id= "@+id/iv"/> </LinearLayout>

Run effect

Java implementation

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The package Com.example.halower.menudemo;   Import a ndroid.app.Activity; Import Android.os.Bundle; Import Android.view.ContextMenu; Import Android.view.MenuInflater; Import Android.view.View; Import Android.widget.ImageView;     public class Mainactivity extends activity {  @Override protected void onCreate (Bundle savedinstancest ATE) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView iv= (ImageView) Findviewbyid (R.id.iamgeview); Iv.setimageresource (r.drawable.test); Registerforcontextmenu (iv); }   @Override public void Oncreatecontextmenu (ContextMenu menu, View V, Contextmenu.contextmenuinfo menuinfo) {Super . Oncreatecontextmenu (menu, V, menuinfo); Menuinflater inflater=new Menuinflater (this); Inflater.inflate (R.menu.menu_main,menu); Menu.setHeadericon (R.drawable.blue); Menu.setheadertitle (R.string.menutitle); } }

Layout

?
1 2 3 4 5 6 7 8 9 10 11 12-13 <?xml version= "1.0" encoding= "Utf-8"?> <resources> <string name= "App_name" >menudemo</string > <string name= "action_settings" >Settings</string> <string name= "item1name" > Menu 1</string > <string name= "item2name" > Menu 2</string> <string name= "item3name" > Menu 3--more</string> < String name= "Item4name" > Menu 4</string> <string name= "Menutitle" >xml parsing menu </string> <string Name= "Subitem1name" > submenu 1</string> <string name= "subitem2name" > submenu 2</string> </resources >
Related Article

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.