Home>

Not much to say, the above picture, see the 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:[email protected]
 * @version time:January 26, 2011 at 04:42:04 pm
 * /
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);
  }
  //The key code is to override the layout.factory.oncreateview () method to customize the layout
  protected void setmenubackground () {
    menuact.this.getlayoutinflater (). setfactory (new android.view.layoutinflater.factory () {
      /**
       * 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 />
       * /
      public view oncreateview (string name, context context, attributeset attrs) {
        //specify a custom inflate object
        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 the background image
                view.setbackgroundresource (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>
  • Previous Detailed fopen () and fdopen () functions in C
  • Next Detailed usage of telldir () function and seekdir () function in C language