Android changes PopupMenu background and displays icons

Source: Internet
Author: User

It seems that PopupMenu is unable to set style alone, as if it is decided by the Context, the first few days need to set style, looked for a long time to find a way, it seems to be through the Contextthemewrapper packaging a context and then the context The constructor passed as a parameter to the PopupMenu

The Java code is as follows

/**************************************************************************/       Context wrapper = new Contextthemewrapper (this, r.style.mypopupstyle);/************************************************************** /       PopupMenu PopupMenu = new PopupMenu (wrapper, view);       Popupmenu.getmenuinflater (). Inflate (R.menu.user_main_toolbar_add_items, Popupmenu.getmenu ());       Popupmenu.show ();

  

The style file is as follows

<style name= "Mypopupstyle" parent= "Widget.AppCompat.PopupMenu" >        <item name= "Android:itembackground" > @drawable/toolbar_item_selector</item>        <item name= "Android:textcolor" > @color/whitesmoke< /item></style>

  

Remember that the use of the background attribute is not valid, changed to Itembackground has the effect

And in order to make PopupMenu can show icon, find a way, and did not check the reason, just used, the code is as follows

/**************************************************************************/     Context wrapper = new Contextthemewrapper (this, r.style.mypopupstyle);/************************************************************** /     PopupMenu menu = new PopupMenu (wrapper, view);     Menu.inflate (r.menu.user_main_toolbar_add_items);     Menupopuphelper menuhelper = new Menupopuphelper (wrapper, (Menubuilder) Menu.getmenu (), view);     Menuhelper.setforceshowicon (true);     Menuhelper.show ();

  

And the official documents do not seem to search this class, Google first is the source code, there are various methods of instructions, including Setforceshowicon,

This article was originally used only for their own reference, but may be seen, or posted links, you can look at the

Menupopuphelper. Java

Android changes PopupMenu background and displays icons

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.