[Android] background of custom System Menu

Source: Internet
Author: User

 

For more information, see the code.

 

 

 

 

Package lab. sodino. menutest; <br/> Import android. content. context; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. util. attributeset; <br/> Import android. view. inflateexception; <br/> Import android. view. layoutinflater; <br/> Import android. view. menu; <br/> Import android. view. menuinflater; <br/> Import android. view. menuitem; <br/> Import android. view. view; <br/> Import android. widget. toast; <br/>/** <br/> * @ author sodino E-mail: sodinoopen@hotmail.com <br/> * @ version time: 04:42:04 <br/> */<br/> public class menuact extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}< br/> Public Boolean oncreateoptionsmenu (menu) {<br/> super. oncreateoptionsmenu (menu); <br/> menuinflater Inflater = new menuinflater (getapplicationcontext (); <br/> Inflater. inflate (R. menu. menu, menu); <br/> setmenubackground (); <br/> return true; <br/>}< br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> string info = ""; <br/> switch (item. getitemid () {<br/> case R. id. menu_add: <br/> info = "add"; <br/> break; <br/> case R. id. menu_delete: <br/> info = "delete"; <br/> break; <br/> case R. id. menu_home: <br/> info = "home"; <br/> break; <br/> case R. id. menu_help: <br/> info = "help"; <br/> break; <br/> default: <br/> info = "null"; <br/> break; <br/>}< br/> Toast = toast. maketext (this, info, toast. length_short); <br/> toast. show (); <br/> return Super. onoptionsitemselected (item); <br/>}< br/> // the key code is to override layout. factory. oncreateview () method custom Layout <br/> protected void setmenubackground () {<br/> menuact. this. getlayoutinflater (). setfactory (new android. view. layoutinflater. factory () {<br/>/** <br/> * name-tag name to be inflated. <br/> * context-the context the view is being created in. <br/> * attrs-inflation attributes as specified in XML file. <br/> */<br/> Public View oncreateview (string name, context, attributeset attrs) {<br/> // specify the custom inflate object <br/> If (name. equalsignorecase ("com. android. internal. view. menu. iconmenuitemview ") {<br/> try {<br/> layoutinflater F = getlayoutinflater (); <br/> final view = f. createview (name, null, attrs); <br/> new handler (). post (New runnable () {<br/> Public void run () {<br/> // set the background image <br/> View. setbackgroundresource (R. drawable. menu_background); <br/>}< br/>}); <br/> return view; <br/>}catch (inflateexception e) {<br/> E. printstacktrace (); <br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/> return NULL; <br/>}< br/> }); <br/>}< br/>}

 

 

/RES/menu. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/menu_add" Android: Title = "add" Android: icon = "@ drawable/menu_add"> </item> <br/> <item Android: Id = "@ + ID/menu_delete" Android: Title = "delete" Android: icon = "@ drawable/menu_delete"> </item> <br/> <item Android: Id = "@ + ID/menu_home" Android: Title = "home" Android: icon = "@ drawable/menu_home"> </item> <br/> <item Android: Id = "@ + ID/menu_help" Android: Title = "help" Android: icon = "@ drawable/menu_help"> </item> <br/> </menu>

This article belongs to the csdn blog master.Sodino
All

Reprinted please indicate the source:
Http://blog.csdn.net/sodino/archive/2011/01/26/6165132.aspx

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.