Your own common Android Options menu style collection _android

Source: Internet
Author: User
Tags stub

Menus are one of the most common elements of the user interface and are used very frequently, and in Android, menus are divided into three options (optionsmenu), Context menus (ContextMenu), and submenus (submenu), which today speak Optionsmenu
I. Overview

    • public boolean Oncreateoptionsmenu (Menu menu): Use this method to invoke Optionsmenu.
    • public boolean onoptionsitemselected (MenuItem Item): The action that occurs when a menu item is selected.
    • public void onoptionsmenuclosed (Menu menu): The action that occurs after a menu is closed.
    • public boolean Onprepareoptionsmenu (Menu menu): The Onprepareoptionsmenu method is called before the Options menu is displayed, and you can use this method to adjust the menu according to the situation at that time.
    • public boolean onmenuopened (int featureid, menu menu): An action that occurs after a single opening.

Second, the default style
The default style is to pop a menu at the bottom of the screen, and we'll call him the menu of options. Optionsmenu, in general, the option menu shows up to 2 rows per row of 3 menu items, these menu items have text icon, also known as Icon Menus, if more than 6 items, From the sixth item will be hidden, in the sixth item will appear a more, click More to appear sixth and later menu items, these items are also called expanded Menus. described below.

<?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 ">
  
  <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "
    android:text= "Please click the menu button to display Options"
    android:id= "@+id/textview02"/>

</LinearLayout>

2, overloaded Oncreateoptionsmenu (Menu menu) method
Overload the Oncreateoptionsmenu (Menu menu) method, add a menu item to this method, and finally return true, and if false, it will not appear.

The @Override public boolean oncreateoptionsmenu (Menu menu) {/* * * * Add () method has four parameters, followed by: * * 1, group, such as The fruit does not divide the words to write menu.none, * * 2, the ID, this is very important, the android according to this ID to determine the different menu * * 3, the order, that menu now in front by this parameter's size decides * * 4, text, menu display text * * * MENU.ADD (Menu.none, Menu.first + 1, 5, "delete"). SetIcon (Android.

    R.drawable.ic_menu_delete); SetIcon () Method for menu Settings icon, here is the system with the icon, students to pay attention to//Android. The resources at the beginning of R are provided by the system, and the resources we provide ourselves are menu.add (Menu.none, Menu.first + 2, 2, "save") starting with R. SetIcon (Android.

    R.drawable.ic_menu_edit); Menu.add (Menu.none, Menu.first + 3, 6, "help"). SetIcon (Android.

    R.DRAWABLE.IC_MENU_HELP); Menu.add (Menu.none, Menu.first + 4, 1, "Add"). SetIcon (Android.

    R.drawable.ic_menu_add); Menu.add (Menu.none, Menu.first + 5, 4, "detail"). SetIcon (Android.

    R.drawable.ic_menu_info_details); Menu.add (Menu.none, Menu.first + 6, 3, "send"). SetIcon (Android.

    R.drawable.ic_menu_send);

  return true;

 }

3. Registering events for menu items
Registering events for menu items using the onoptionsitemselected (MenuItem Item) method

@Override Public
  Boolean onoptionsitemselected (MenuItem item) {
    switch (Item.getitemid ()) {

    case Menu.first + 1:

      toast.maketext (This, "delete menu was clicked", Toast.length_long). Show ();

      break;

    Case Menu.first + 2:

      toast.maketext (This, "Save menu was clicked", Toast.length_long). Show ();

      break;

    Case Menu.first + 3:

      Toast.maketext (This, "Help menu was clicked", Toast.length_long). Show ();

      break;

    Case Menu.first + 4:

      toast.maketext (This, "Add menu was clicked", Toast.length_long). Show ();

      break;

    Case Menu.first + 5:

      toast.maketext (This, "detailed menu was clicked", Toast.length_long). Show ();

      break;

    Case Menu.first + 6:

      Toast.maketext (This, "Send menu was clicked", Toast.length_long). Show ();

      break;

    return false;

  }

4, other as needed overload
Complete code

Package com.wjq.menu;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;

Import Android.widget.Toast; The public class Defaultmenu extends activity {/** called the ' when the ' is the ' The activity ' is a./@Override public void
    OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main); @Override public boolean Oncreateoptionsmenu (Menu menu) {/* * * * Add () method four parameters, followed by: * * 1, 
     Group, if not grouped to write Menu.none, * * 2, Id, this is very important, Android based on this ID to determine the different menu * * * 3, the order, the menu is now in front by the size of this parameter decision * * 4, text, menu display text * * MENU.ADD (Menu.none, Menu.first + 1, 5, "delete"). SetIcon (Android.

    R.drawable.ic_menu_delete); SetIcon () Method for menu Settings icon, here is the system with the icon, students to pay attention to//Android. The resources at the beginning of R are provided by the system, and the resources we provide ourselves are menu.add (Menu.none, Menu.first + 2, 2, "save") starting with R. SetIcon (Android.

    R.drawable.ic_menu_edit); Menu.add (Menu.none, Menu.first + 3, 6, "help"). SetIcon (Android.

    R.DRAWABLE.IC_MENU_HELP); Menu.add (Menu.none, Menu.first + 4, 1, "Add"). SetIcon (Android.

    R.drawable.ic_menu_add); Menu.add (Menu.none, Menu.first + 5, 4, "detail"). SetIcon (Android.

    R.drawable.ic_menu_info_details); Menu.add (Menu.none, Menu.first + 6, 3, "send"). SetIcon (Android.

    R.drawable.ic_menu_send);

  return true; @Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case Menu.firs

      T + 1:toast.maketext (this, "delete menu was clicked", Toast.length_long). Show ();

    Break

      Case Menu.first + 2:toast.maketext (This, "Save menu was clicked", Toast.length_long). Show ();

    Break

      Case Menu.first + 3:toast.maketext (This, "Help menu was clicked", Toast.length_long). Show ();

    Break

      Case Menu.first + 4:toast.maketext (This, "Add menu was clicked", Toast.length_long). Show ();

    Break Case Menu.first + 5:toast.maketext (This, "detailed menu was clicked", Toast.length_long). Show ();

    Break

      Case Menu.first + 6:toast.maketext (this, "Send menu was clicked", Toast.length_long). Show ();

    Break

  return false; @Override public void onoptionsmenuclosed (this, the Options menu is turned off, Toast.length_long). Show (
  ); @Override public boolean Onprepareoptionsmenu (Menu menu) {Toast.maketext (the This, "option menu is displayed before Onprepareoptio

    The Nsmenu method is invoked, and you can use this method to adjust the menu ", Toast.length_long," according to the situation at the time. Show ();

  If False, this method consumes the user's action to click the menu, and the Oncreateoptionsmenu method will not be called return true;

 }
}

5. Effect browsing

  

Three, custom style

<?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"
  >
<gridview
     android:id= "@+id/gridview" android:layout_width= "Fill_"
     Parent "
     android:layout_height=" fill_parent "
     android:numcolumns=" 4 "
     android:verticalspacing=" 10dip "
     android:horizontalspacing=" 10dip "
     android:stretchmode=" columnWidth "
     android:gravity=" Center "
     />
 
</LinearLayout>

First customize the menu interface, I am the GridView to include the menu items, 4 columns, 3 rows

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:id=" @+id/relativelayout_item "
  android:layout_width=" fill_parent "Android:layout_" height= "Wrap_content"
  android:paddingbottom= "5dip" >
  <imageview android:id= "@+id/item_image"
    Android:layout_centerhorizontal= "true" android:layout_width= "wrap_content"
    android:layout_height= "WRAP_" Content "></ImageView>
  <textview android:layout_below=" @id/item_image "android:id=" @+id/item_text "
    android:layout_centerhorizontal=" true "android:layout_width=" wrap_content "
    android:layout_height=" Wrap_content "android:text=" Options ></TextView>
</RelativeLayout>

The realistic style of a menu item, an icon and a text.

3. Define

Private Boolean Ismore = false;//Menu Paging control alertdialog menudialog;//Menu Menu dialog GridView Menugrid;
  
  View Menuview; Private final int item_search = 0;//search private final int item_file_manager = 1;//file Management private final int item_down_ma Nager = 2;//Download Management private final int item_fullscreen = 3;//full-screen private final int item_more = 11;//Menu/** Menu Picture * *
      /int[] Menu_image_array = {r.drawable.menu_search, R.drawable.menu_filemanager, R.drawable.menu_downmanager, R.drawable.menu_fullscreen, R.drawable.menu_inputurl, R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_imp
      ORT, R.drawable.menu_sharepage, R.drawable.menu_quit, R.drawable.menu_nightmode, R.drawable.menu_refresh,
  R.drawable.menu_more}; /** Menu Text **/string[] Menu_name_array = {"Search", "File Management", "Download Management", "full Screen", "url", "Bookmark", "Add Bookmark", "Share Page", "Exit", "Night Mode", "Brush
  New "," more "}; /** Menu Picture 2 **/int[] Menu_image_array2 = {R.drawable.menu_auto_landscape, r.drawable.menU_penselectmodel, R.drawable.menu_page_attr, R.drawable.menu_novel_mode, R.drawable.menu_page_updown, R.drawab Le.menu_checkupdate, R.drawable.menu_checknet, R.drawable.menu_refreshtimer, R.drawable.menu_syssettings, R.dr
  Awable.menu_help, R.drawable.menu_about, R.drawable.menu_return}; /** Menu Text 2 **/string[] Menu_name_array2 = {"Automatic horizontal screen", "Pen mode", "Reading Mode", "Browse mode", "Quick paging", "Check Update", "Check Network", "Timed Refresh", "Settings",


"Help", "about", "return"}; @Override public boolean onmenuopened (int featureid, menu menu) {if (Menudialog = null) {Menudialog = new A
    Lertdialog.builder (This). Setview (Menuview). Show ();
    else {menudialog.show ();

 Return false;//to True to show system menu}

If you set the view the first time you open it, the Menudialog view is displayed directly.

Private Simpleadapter Getmenuadapter (string[] Menunamearray,
      int[] imageresourcearray) {
    arraylist< hashmap<string, object>> data = new arraylist 
 

Adds a menu item to the menu.

@Override public boolean Oncreateoptionsmenu (Menu menu) {menu.add ("menu");//You must create a return super.oncreateoptio
  Nsmenu (menu); @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.on
    
    Create (savedinstancestate);
    
    Setcontentview (R.layout.main);
    Menuview = View.inflate (this, r.layout.gridview_menu, null);
    Creates alertdialog Menudialog = new Alertdialog.builder (this). Create ();
    Menudialog.setview (Menuview);
          Menudialog.setonkeylistener (New Onkeylistener () {public boolean OnKey (dialoginterface dialog, int keycode,
        KeyEvent event) {if (keycode = = keyevent.keycode_menu)/monitor button Dialog.dismiss ();
      return false;

    }
    });
    Menugrid = (GridView) Menuview.findviewbyid (R.id.gridview);
    Menugrid.setadapter (Getmenuadapter (Menu_name_array, Menu_image_array)); /** Listener menu option **/menugrid.setonitemclicklistener (new Onitemclicklistener () {
      public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {switch (arg2
        ) {case item_search://search break;
        Case item_file_manager://file Management break;
        Case item_down_manager://Download Management break;
        Case item_fullscreen://full screen break;
                Case item_more://Page-turn if (ismore) {menugrid.setadapter (Getmenuadapter menu_name_array2,
            Menu_image_array2));
          Ismore = false;
            else {//home Menugrid.setadapter (Getmenuadapter (Menu_name_array, Menu_image_array));
          Ismore = true;
          } menugrid.invalidate ()//Update menu menugrid.setselection (Item_more);
        Break
  }
        
        
      }
    });

 }

Package com.wjq.menu;
Import java.util.ArrayList;

Import Java.util.HashMap;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnKeyListener;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.GridView;
Import Android.widget.SimpleAdapter;

Import Android.widget.AdapterView.OnItemClickListener; public class Customizemenu extends activity {Private Boolean Ismore = false;//Menu Paging control Alertdialog menudialog;/
  /Menu Dialog GridView Menugrid;
  
  View Menuview; Private final int item_search = 0;//search private final int item_file_manager = 1;//file Management private final int item_down_ma Nager = 2;//Download Management private final int item_fullscreen = 3;//full-screen private final int item_more = 11;//Menu/** Menu Picture * * /int[] Menu_image_array = {R.drawable.menu_search, R.DRAWABLE.MEnu_filemanager, R.drawable.menu_downmanager, R.drawable.menu_fullscreen, R.drawable.menu_inputurl, R.drawable . Menu_bookmark, R.drawable.menu_bookmark_sync_import, R.drawable.menu_sharepage, R.drawable.menu_quit, R.drawa
  Ble.menu_nightmode, R.drawable.menu_refresh, r.drawable.menu_more}; /** Menu Text **/string[] Menu_name_array = {"Search", "File Management", "Download Management", "full Screen", "url", "Bookmark", "Add Bookmark", "Share Page", "Exit", "Night Mode", "Brush
  New "," more "}; /** Menu Picture 2 **/int[] Menu_image_array2 = {r.drawable.menu_auto_landscape, R.drawable.menu_penselectmodel, R.drawab Le.menu_page_attr, R.drawable.menu_novel_mode, R.drawable.menu_page_updown, R.drawable.menu_checkupdate, R.dra Wable.menu_checknet, R.drawable.menu_refreshtimer, R.drawable.menu_syssettings, R.drawable.menu_help, R.drawab
  Le.menu_about, R.drawable.menu_return}; /** Menu Text 2 **/string[] Menu_name_array2 = {"Automatic horizontal screen", "Pen mode", "Reading Mode", "Browse mode", "Quick paging", "Check Update", "Check Network", "Timed Refresh", "Settings", "Help", "about","Back"}; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
    
    (savedinstancestate);
    
    Setcontentview (R.layout.main);
    Menuview = View.inflate (this, r.layout.gridview_menu, null);
    Creates alertdialog Menudialog = new Alertdialog.builder (this). Create ();
    Menudialog.setview (Menuview);
          Menudialog.setonkeylistener (New Onkeylistener () {public boolean OnKey (dialoginterface dialog, int keycode,
        KeyEvent event) {if (keycode = = keyevent.keycode_menu)/monitor button Dialog.dismiss ();
      return false;

    }
    });
    Menugrid = (GridView) Menuview.findviewbyid (R.id.gridview);
    Menugrid.setadapter (Getmenuadapter (Menu_name_array, Menu_image_array)); /** Listener menu option **/menugrid.setonitemclicklistener (new Onitemclicklistener () {public void Onitemclick (Adapterview
        <?> arg0, View arg1, int arg2, long arg3) {switch (ARG2) {Case item_search://Search break;
        Case item_file_manager://file Management break;
        Case item_down_manager://Download Management break;
        Case item_fullscreen://full screen break;
                Case item_more://Page-turn if (ismore) {menugrid.setadapter (Getmenuadapter menu_name_array2,
            Menu_image_array2));
          Ismore = false;
            else {//home Menugrid.setadapter (Getmenuadapter (Menu_name_array, Menu_image_array));
          Ismore = true;
          } menugrid.invalidate ()//Update menu menugrid.setselection (Item_more);
        Break
  }
        
        
      }
    }); @Override public boolean Oncreateoptionsmenu (Menu menu) {menu.add ("menu");//must create a return Super.oncreateop
  Tionsmenu (menu); Private Simpleadapter Getmenuadapter (string[] Menunamearray, int[] imageresourcearray) {Arraylist 

Effect Browsing:

The above is the entire content of this article, I hope to help you learn.

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.