In the Android4.0 system, creating menu menus and then adding icons to the menu through the SetIcon method is not valid, the icons will not appear, and the previous system can be displayed. The root cause of this problem lies in the 4.0 system, which involves the menu source class Menubuilder has changed, which has private Boolean moptionaliconsvisible = false; Property causes the menu icon to not be displayed.
However, it is not possible to create a Menubuilder object in a developed application (because Menubuilder is a framework class inside the system).
Of course, if it is not necessary to be very recommended, or in another way. If it has to be used and must be used in a seticon way, it is only through the reflection mechanism to implement the call Setoptionaliconsvisible method setting Moptionaliconsvisible to True
//Write your own Settings menu icon effective function //The parameter menu is the parameter menu in Oncreateoptionsmenu (Menu menu). //so the function is to be used within it and to be called before you use SetIcon (). Private voidSeticonenable (Menu menu,Booleanenable) { Try{Class<?> C = class.forname ("Com.android.internal.view.menu.MenuBuilder"); Method m= C.getdeclaredmethod ("Setoptionaliconsvisible",Boolean.class); M.setaccessible (true); M.invoke (menu, enable); } Catch(Exception e) {e.printstacktrace (); } }
about troubleshooting menus in android4.0 system cannot add icon