Android is learning-menu background image and background color settings

Source: Internet
Author: User

After studying this afternoon, I finally finished setting the background image and background color of the menu. The results are as follows.

I don't care about it. It's just for reference. Let's take a look. The Code is as follows:

 

Package COM. hooypay. activity; import android. app. alertdialog; import android. content. context; import android. content. dialoginterface; import android. content. intent; import android. graphics. color; import android. OS. handler; import android. util. attributeset; import android. util. log; import android. view. inflateexception; import android. view. layoutinflater; import android. view. menu; import android. view. menuitem; Im Port android. view. view; import android. view. layoutinflater. factory; public class menuactivity extends basegroupactivity {private intent; Private Static final string tag = "debug"; // create a menu. The activity calls the oncreateoptionsmenu (menu) method, upload a menu object that implements the menu interface for use. @ Override public Boolean oncreateoptionsmenu (menu) {// menu. Add () contains four parameters: // first, group. // Second, id. Is the menu identification number for identifying the menu. Important. // The third, in sequence. The size of this parameter determines the order in which the menu appears. The order is from small to big. The menu is arranged from left to right and from top to bottom. A maximum of three rows can be created. // Fourth, display text. Menu. add (0, menu. first, 0, "Homepage "). seticon (R. drawable. conf_home); menu. add (0, menu. first + 1,1, "My orders "). seticon (Android. r. drawable. ic_menu_save); menu. add (0, menu. first + 2, "personal information "). seticon (Android. r. drawable. ic_menu_send); menu. add (1, menu. first + 3, 3, "more "). seticon (R. drawable. conf_more); menu. add (1, menu. first + 4, 4, "quit "). seticon (Android. r. drawable. ic_menu_delete); menu. add (1, menu. first + 5, "help "). seticon (Android. R. drawable. ic_menu_help); setmenubackground (); intent = new intent (); Return true;} // sets the background of the menu protected void setmenubackground () {log. D (TAG, "enterting setmenubackground"); getlayoutinflater (). setfactory (New Factory () {@ override public view oncreateview (string name, context, attributeset attrs) {If (name. equalsignorecase ("com. android. internal. view. menu. iconmenuitemview ") {T Ry {// ask our Inflater to create the view layoutinflater F = getlayoutinflater (); final view = f. createview (name, null, attrs); new handler (). post (New runnable () {public void run () {// view. setbackgroundresource (R. drawable. menu_backg); // sets the background image view. setbackgroundcolor (new color (). green); // set the background color}); Return view;} catch (inflateexception e) {} catch (classnotfoundexception e ){}} Return NULL ;}}) ;}// select event @ override public Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {Case menu. first: intent. setclass (menuactivity. this, mainactivity. class); throudintent (intent); break; Case menu. first + 1: intent. setclass (menuactivity. this, orderactivity. class); throudintent (intent); break; Case menu. first + 2: intent. setclass (menuactivity. this, userinfoactivity. Class); throudintent (intent); break; Case menu. first + 3: intent. setclass (menuactivity. this, hometabgroup. class); throudintent (intent); break; Case menu. first + 4: isexit (); break; Case menu. first + 5: // toast. maketext (this, "the Help menu has been clicked", toast. length_short ). show (); break;} return false;} // The previous event @ override public Boolean onprepareoptionsmenu (menu) {// toast. maketext (this, "events before the menu is displayed, you can adjust the menu here", AST. length_long). Show (); Return true; // you must return true to call the oncreateoptionsmenu (menu) method.} // Select the Close event of the menu. // There are three ways to close the menu, the menu button is clicked again, the back button is clicked, and the user selects another menu item. @ Override public void onoptionsmenuclosed (menu) {// toast. maketext (this, "the menu is closed", toast. length_short ). show () ;}// jump to other pages. Private void throudintent (intent) {startactivity (intent) ;}// the system prompts whether to exit public void isexit () {alertdialog. builder alertbbuilder = new alertdialog. builder (this); alertbbuilder. seticon (R. drawable. infoicon ). settitle ("prompt... "). setmessage ("are you sure you want to leave the client? "); Alertbbuilder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {intent = new intent (); intent. putextra ("exit", "exit"); // Add a parameter, which is based on intent. setclass (menuactivity. this, loginactivity. class); // jump to the login interface and exit intent according to the parameters. setflags (intent. flag_activity_clear_top); // pay attention to the flag settings of the row. Clear all activity records startactivity (intent); // Note: Check and exit on the redirected page}); alertbbuilder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {dialog. cancel ();}}). create (); alertbbuilder. show ();}}

The above code is built on others' code. He didn't write any background. I added it.

I hope that some suggestions will be helpful to my progress.

From: http://www.cnblogs.com/winxiang/archive/2012/02/27/2370257.html

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.