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