Chapter II Example: Android Window menu display

Source: Internet
Author: User

 PackageTest.main.cls;ImportCOM.EXAMPLE.POPUPWINDOW.R;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.ContextMenu;ImportAndroid.view.ContextMenu.ContextMenuInfo;Importandroid.view.Gravity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.SubMenu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.widget.Button;ImportAndroid.widget.PopupMenu;ImportAndroid.widget.PopupMenu.OnMenuItemClickListener;ImportAndroid.widget.PopupWindow;ImportAndroid.widget.Toast; Public classPopupwindowsextendsActivity {@Override Public voidOncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {Menu.setheadericon (R.draw        Able.ic_launcher); Submenu submenu= Menu.addsubmenu ("system fix");//This method returns the submenu interface object, presumably the menu implements the submenu interfaceSubmenu.add (0, 0, 0, "Auto Fix"). SetIcon (R.drawable.ic_launcher); Submenu.add (0, 1, 0, "Repair external memory"). SetIcon (R.drawable.ic_launcher); Submenu.add (0, 2, 0, "Repair hardware"). SetIcon (R.drawable.ic_launcher); //------------------------------------------Menu.setheadericon (R.drawable.ic_launcher); submenu submenus= Menu.addsubmenu ("global Fix");//This method returns the submenu interface object, presumably the menu implements the submenu interfaceSubmenus.add (0, 0, 0, "full repair"). SetIcon (R.drawable.ic_launcher); Submenus.add (0, 1, 0, "non-destructive repair"). SetIcon (R.drawable.ic_launcher); Submenus.add (0, 2, 0, "perfect fix"). SetIcon (R.drawable.ic_launcher); Super. Oncreatecontextmenu (menu, V, menuinfo); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {submenu submenu= Menu.addsubmenu ("Destruction system");//This method returns the submenu interface object, presumably the menu implements the submenu interfaceSubmenu.add (0, 0, 0, "total destruction"). SetIcon (R.drawable.ic_launcher); Submenu.add (0, 1, 0, "Destroy external memory"). SetIcon (R.drawable.ic_launcher); Submenu.add (0, 2, 0, "hardware Destruction"). SetIcon (R.drawable.ic_launcher); //submenu.add (0, 4, 0, "destruction battery"). SetIcon (R.drawable.ic_launcher). Setintent (/*intent*/); //submenu.add (0, 3, 0, "destruction screen"). Setshortcut (' F ', ' 5 '); //submenu.setgroupcheckable (0, True, true); whether the button group can be checked//-----------------------------------------submenu submenu1 = Menu.addsubmenu ("Send scam Information"); Submenu1.add (0, 0, 0, "Send to all Friends"); Submenu1.add (0, 1, 0, "Send to nearest contact"); Submenu1.add (0, 2, 0, "random send"); Submenu1.add (0, 3, 0, "Send Abroad"); return Super. Oncreateoptionsmenu (menu); } @Override Public voidonoptionsmenuclosed (Menu menu) {//TODO auto-generated Method Stub        Super. onoptionsmenuclosed (menu); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); View Contentv= This. Getlayoutinflater (). Inflate (R.layout.main_layout,NULL);  This. Setcontentview (CONTENTV); //-------------------------------------------Button BTN =(Button) Findviewbyid (R.ID.CLICKSSSS); FinalPopupMenu Pmenu =NewPopupMenu (popupwindows. This, BTN);  This. Getmenuinflater (). Inflate (R.menu.popup_menu, Pmenu.getmenu ()); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {pmenu.show ();        }        }); Pmenu.setonmenuitemclicklistener (NewOnmenuitemclicklistener () {@Override Public BooleanOnmenuitemclick (MenuItem Item) {Toast.maketext (popupwindows. This, Item.gettitle (), Toast.length_long). Show (); return true;        }        }); ///------------------------------------------        //Note: Findviewbyid requires setcontentview after use         This. Registerforcontextmenu (CONTENTV);//to a View binding context menu}}

Xml:
main_layout<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android " android:layout_width=" Match_parent " android:layout_ Height= "match_parent" android:orientation= "vertical" > <button Android:id = "@+id/clickssss" android:layout_width= "Match_parent" android:layout_height = "Wrap_content" android:text= "Eject"/></linearlayout>
xml: // menu/popup_menu.xml<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "/http Schemas.android.com/apk/res/android ">    <group android:checkablebehavior=" None ">        <item Android : id= "@+id/menu_1"            android:title= "This is a popup menu"/>        <item android:id= "@+id/menu_2"             Android:title= "Its usage is also very simple"/>        <item android:id= "@+id/menu_3"            android:title= " Control use is not the focus "/>        <item android:id=" @+id/menu_4 "            android:title=" Focus is to understand the code "/>                 </ Group></menu>

Chapter II Example: Android Window menu display

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.