Copy Code code as follows:
public class Menuex extends activity {
private static final String TAG = "android123";
@Override public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
@Override public boolean Oncreateoptionsmenu (Menu menu) {
Super.oncreateoptionsmenu (menu);
Menuinflater inflater = new Menuinflater (Getapplicationcontext ());
Inflater.inflate (R.menu.options_menu, menu);
Setmenubackground ();
return true;
}
The key code is to rewrite the factory method of the layout class Oncreateview, where you can replace the view that is drawn in the system
Copy Code code as follows:
protected void Setmenubackground () {
LOG.D (TAG, "start setting the background of the menu");
Getlayoutinflater (). Setfactory (New Factory () {
@Override public View Oncreateview (String name, context context, AttributeSet Attrs) {
if (Name.equalsignorecase ("Com.android.internal.view.menu.IconMenuItemView")) {
The above sentence Android123 to remind you not to change, at present, for native Android this packagename has not changed
try {
Layoutinflater f = getlayoutinflater ();
Final View view = F.createview (name, null, ATTRS); Try to create our own layout
New Handler (). Post (new Runnable () {
public void Run () {
View.setbackgroundresource (R.DRAWABLE.CWJ "_BG"); Set the background for our custom picture, replace the Cwj_bg file can}};
return view;
} catch (
Inflateexception e) {}
catch (
ClassNotFoundException e) {}
}
return null;
} }); }}
The above example can easily replace the menu background color of the current activity, where the Android development network once again reminds you that the bold package name cannot be changed at will, and if it is not native to Android, this may be flexibly handled according to the firmware compiled by each vendor.