Android Development Series (28): Use SubMenu to create option menus

Source: Internet
Author: User

Android Development Series (28): Use SubMenu to create option menus

Most mobile phones have a "MENU" key. After an application is installed on a mobile phone, you can use "MENU" to display the menus associated with the application.

However, since Android 3.0, Android does not require that menus be provided on mobile devices. Although many mobile phones still provide MENU buttons, some of them are no longer provided. In this case, Android recommends using ActionBar instead of menu. In future blog posts, we will introduce Android's support for ActionBar.


The Menu interface is one of the buttons to implement this function. The SubMenu inherits from the Menu parent interface.


Next, we will use a specific application to describe most of the Methods:

First, create a main. xml file:

 
 
  
 
In this file, we define a line of text. Next we will change the font and font color of the line text through menus and submenus.


Next, let's take a look at MenuTest. java:

Import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. widget. editText; import android. widget. toast; public class MenuTest extends Activity {// The identifier of the menu item defining the font size. final int FONT_10 = 0x111; final int FONT_12 = 0x112; final int FONT_14 = 0x113; final int FONT_16 = 0x114; final int FONT_18 = 0x115; // define the final int PLAIN_ITEM = 0x11b; // define the final int FONT_RED = 0x116; final int FONT_BLUE = 0x117; final int FONT_GREEN = 0x118; private EditText edit; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); edit = (EditText) findViewById(R.id.txt);} // This method is triggered when the user clicks the MENU key @ Overridepublic boolean onCreateOptionsMenu (Men U menu) {// ------------- Add a font size sub-menu to menu ----------- SubMenu fontMenu = menu. addSubMenu ("font size"); // set the fontMenu icon of the menu. setIcon (R. drawable. font); // set the fontMenu icon in the menu header. setHeaderIcon (R. drawable. font); // set the title fontMenu of the menu header. setHeaderTitle ("select font size"); fontMenu. add (0, FONT_10, 0, "font 10"); fontMenu. add (0, FONT_12, 0, "12 font"); fontMenu. add (0, FONT_14, 0, "font 14"); fontMenu. add (0, FONT_16, 0, "font 16"); fontMenu. add (0, FONT_18, 0, "font 18"); // ------------- Add the normal menu item ----------- menu to the menu. add (0, PLAIN_ITEM, 0, "normal menu item"); // ------------- add a text color SubMenu to menu ------------- SubMenu colorMenu = menu. addSubMenu ("font color"); colorMenu. setIcon (R. drawable. color); // set the colorMenu icon in the menu header. setHeaderIcon (R. drawable. color); // set the title colorMenu of the menu header. setHeaderTitle ("select text color"); colorMenu. add (0, FONT_RED, 0, "Red"); colorMenu. add (0, FONT_GREEN, 0, "green"); col OrMenu. add (0, FONT_BLUE, 0, "blue"); return super. onCreateOptionsMenu (menu) ;}@ Override // callback method after the menu item of the option menu is clicked public boolean onOptionsItemSelected (menumi) {// you can determine which menu item to click, and make targeted responses. Switch (mi. getItemId () {case FONT_10: edit. setTextSize (10*2); break; case FONT_12: edit. setTextSize (12*2); break; case FONT_14: edit. setTextSize (14*2); break; case FONT_16: edit. setTextSize (16*2); break; case FONT_18: edit. setTextSize (18*2); break; case FONT_RED: edit. setTextColor (Color. RED); break; case FONT_GREEN: edit. setTextColor (Color. GREEN); break; case FONT_BLUE: edit. setTextColor (Color. BLUE); break; case PLAIN_ITEM: Toast toast = Toast. makeText (MenuTest. this, "you have clicked the normal menu item", Toast. LENGTH_SHORT); toast. show (); break;} return true ;}}
First, we override the onCreateOptionsMenu (Menu menu) method. This method is triggered when you click the MENU key:

The menu here is the corresponding MENU when the menu key is pressed. addSubMenu (CharSequence title) adds two menus: "font size" and "font color", and adds a common menu item through add (int titleRes ).

Then, we add options for "font size" and "font color" respectively.


Let's take a look:

First, click "MENU" to get the following information:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + signature + cda-vcd4kpha + signature/Signature + Gz9s/Wz8Kx37XE0Ke5 + 828o7o8L3A + Signature = "http://www.2cto.com/uploadfile/Collfiles/20141031/2014103109013676.jpg" alt = "">

Click an option to change the font of the text.












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.