Resolves an invalid menu add icon in the android4.0 system

Source: Internet
Author: User

In the Android4.0 system, creating menu menus, using the SetIcon method to add icons to the menu is not valid, the icon is not shown, 2.3 system can be displayed. The root cause of this problem lies in the 4.0 system, the source of the menu related to the Menubuilder has been changed, the part of the source code is as follows:

1  Public classMenubuilderImplementsMenu {2 ...  3 Private BooleanMoptionaliconsvisible =false; 4 ....  5     voidSetoptionaliconsvisible (Booleanvisible) {  6Moptionaliconsvisible =visible; 7     }  8   9     Booleangetoptionaliconsvisible () {Ten         returnmoptionaliconsvisible;  One     }   A ...   -}

In the above code, the moptionaliconsvisible member initial value defaults to False, which is why the menu setting icon has no effect; As long as we create the menu by calling the Setoptionaliconsvisible method to set Moptionaliconsvisible to True, then the problem is that to invoke the method, you need to create the Menubuilder object, but We are unable to create the Menubuilder object in the developed application (Menubuilder is the framework class inside the system);

It's time to consider reflection, call the Setoptionaliconsvisible method by reflection when the code runs to create the menu, set Moptionaliconsvisible to True, and then add an icon to the menu, This allows you to display the added icon in the menu.

The code is implemented as follows:

1  PackageCom.stevenhu.mie; 2   3 ImportJava.lang.reflect.Method; 4   5 Importandroid.app.Activity; 6 ImportAndroid.os.Bundle; 7 ImportAndroid.util.Log; 8 ImportAndroid.view.Menu; 9 ImportAndroid.view.MenuItem; Ten    One  Public classMainactivityextendsActivity A {   -     /**Called when the activity is first created.*/   - @Override the      Public voidonCreate (Bundle savedinstancestate) -     {   -         Super. OnCreate (savedinstancestate);  - Setcontentview (R.layout.main);  +     }   -    + @Override A      Public BooleanOncreateoptionsmenu (Menu menu) at     {   -         //at run time, the parameter menu is actually the Menubuilder object -LOG.D ("Mainactivity", "Menu--->" +menu);  -            -         /*use the reflection mechanism to invoke the Setoptionaliconsvisible method of Menubuilder to set Moptionaliconsvisible to True, - * only visible when setting icons for menus in          */   -Seticonenable (menu,true);  to            +MenuItem item1 = menu.add (0, 1, 0, R.STRING.ITEM1);  - Item1.seticon (R.drawable.camera);  the            *MenuItem item2 = menu.add (0, 1, 0, R.STRING.ITEM2);  $ Item2.seticon (r.drawable.dial); Panax Notoginseng            -MenuItem item3 = menu.add (0, 1, 0, R.STRING.ITEM3);  the Item3.seticon (r.drawable.sms);  +            A         return Super. Oncreateoptionsmenu (menu);  the     }   +    - @Override $      Public BooleanOnprepareoptionsmenu (Menu menu) $     {   -         //TODO auto-generated Method Stub -         return Super. Onprepareoptionsmenu (menu);  the     }   -       Wuyi     //when enable is true, the menu add icon is valid and the enable is false when it is not valid. 4.0 system default is not valid the     Private voidSeticonenable (Menu menu,Booleanenable) -     {   Wu         Try    -         {   AboutClass<?> clazz = Class.forName ("Com.android.internal.view.menu.MenuBuilder");  $Method m = Clazz.getdeclaredmethod ("Setoptionaliconsvisible",Boolean.class);  -M.setaccessible (true);  -                -             //Menubuilder Implement the menu interface, when creating menus, the passed-in menu is actually the Menubuilder object (Java polymorphic feature) A M.invoke (menu, enable);  +                the}Catch(Exception e) -         {   $ E.printstacktrace ();  the         }   the     }   the}

With the above code, you can implement the icon in the Android4.0 system to set the menu to be effective.

Resolves an invalid menu add icon in the android4.0 system

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.