Android custom Android Menu background code _android

Source: Internet
Author: User

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.

Related Article

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.