Androidmenu background image and background color settings

Source: Internet
Author: User

In the project, I used to change the background image and background color of the custom androidmenu. I posted an article on the Internet as follows: This article is reprinted from the elephant in the pond.

After the background color is changed, the effect is as follows:

DetailsCodeAs follows:

 
Public Boolean oncreateoptionsmenu (menu) {// menuinflater mmenuinf = getmenuinflater (); // mmenuinf. inflate (R. menu. main_menu, menu); menu. add (0, R. id. main_menu_about, 0, R. string. main_menu_about ). seticon (R. drawable. menu_icon_about); menu. add (0, R. id. main_menu_exit, 1, R. string. main_menu_exit ). seticon (R. drawable. menu_icon_exit); setmenubackgroudcolor (); Return true ;}
 
Public Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {case R. id. main_menu_about: break; case R. id. main_menu_exit: // exit the system // cancel the notification // icationicationmanager. cancel (R. drawable. logo_icon_16); dialogutil. exitdialog (myhomeactivity. this, icationicationmanager); break;} return true ;}

You can change the color as follows:

 
Private void setmenubackgroudcolor () {getlayoutinflater (). setfactory (New Factory () {public view oncreateview (string name, context, attributeset attrs) {If (name. equalsignorecase ("com. android. internal. view. menu. iconmenuitemview ") | Name. equalsignorecase ("com. android. internal. view. menu. listmenuitemview ") {try {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 (color. parsecolor ("# f6f9fe"); // sets the background color});} catch (inflateexception e) {e. printstacktrace ();} catch (classnotfoundexception e) {e. printstacktrace () ;}} return NULL ;}});}

that's it... But I did not test it! I think it should be feasible!

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.