The method of customizing system menu background by Android programming _android

Source: Internet
Author: User

This article illustrates the method of Android programming to implement custom system menu backgrounds. Share to everyone for your reference, specific as follows:

Not much to say, above figure, see Code.

Package lab.sodino.menutest;
Import Android.content.Context;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import android.view.InflateException;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.MenuInflater;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.Toast;  /** * @author Sodino e-mail:sodinoopen@hotmail.com * @version time:2011-1-26 PM 04:42:04 * * public class Menuact extends
    Activity {@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
    public boolean Oncreateoptionsmenu (Menu menu) {super.oncreateoptionsmenu (menu);
    Menuinflater inflater = new Menuinflater (Getapplicationcontext ());
    Inflater.inflate (R.menu.menu, menu);
    Setmenubackground ();
  return true;
   public boolean onoptionsitemselected (MenuItem item) {String info = ""; Switch (Item.getitemid ()) {Case r.id.menu_add:info = "add";
    Break
      Case r.id.menu_delete:info = "delete";
    Break
      Case r.id.menu_home:info = "Home";
    Break
      Case r.id.menu_help:info = "help";
    Break
      Default:info = "NULL";
    Break
    } Toast Toast = Toast.maketext (this, info, toast.length_short);
    Toast.show ();
  return super.onoptionsitemselected (item); }//Key code for overriding Layout.Factory.onCreateView () method custom layout protected void Setmenubackground () {MenuAct.this.getLayoutInflat
       ER (). setfactory (New Android.view.LayoutInflater.Factory () {/** * Name-tag name to is inflated.<br/>  * Context-the The view is being created in.<br/> * attrs-inflation attributes as specified
        In XML file.<br/> */Public View Oncreateview (String name, context context, AttributeSet Attrs) { Specifies the object of the custom inflate if (Name.equalsignorecase ("CoM.android.internal.view.menu.iconmenuitemview ")) {try {layoutinflater F = getlayoutinflater ();
            Final View view = F.createview (name, null, ATTRS); New Handler (). Post (new Runnable () {public void run () {//Set background picture View.setba
              Ckgroundresource (R.drawable.menu_background);
            }
            });
          return view;
          catch (Inflateexception e) {e.printstacktrace ();
          catch (ClassNotFoundException e) {e.printstacktrace ();
      } return null;
  }
    });

 }
}

/res/menu/menu.xml

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <item android:id=" @+id/menu_add "android:title=" Add "android:icon=" @drawable/menu_add "> </item>
  <item android:id= "@+id/menu_delete" android:title= "delete" android:icon= "@drawable/menu_ Delete "></item>
  <item android:id=" @+id/menu_home "android:title=" Home "android:icon=" @drawable Menu_home "></item>
  <item android:id=" @+id/menu_help "android:title=" Help "android:icon=" @drawable /menu_help "></item>
</menu>

I hope this article will help you with the Android program.

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.