Questions about resolving menus in android4.0 system cannot add icon

Source: Internet
Author: User

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

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.