Is it possible to display icons in a PopupMenu?
2014-11-17 10:45
309 查看
I really like the new PopupMenu we got in 3.0, but I just can't display any icons next to the menu items in it. I'm inflating the menu from the .xml below:
If you're willing to be a bit adventurous, look at Google's source code for PopupMenu. Create your own class i.e. MyPopupMenu that is the same as Google's PopupMenu class, but make one slight change.
In PopupMenu's constructor:
use the method setForceShowIcon to force it to show the icon. You can also just expose a public method to set this flag as well depending on your needs.
Contribution to the solution provided by Gaelan Bolger. Use this code if you get a "IllegalAccessException: access to field not allowed".
I was able to show the icons using reflection. It may not be the most elegant solution but it works.
http://stackoverflow.com/questions/6805756/is-it-possible-to-display-icons-in-a-popupmenu
<item android:id="@+id/menu_delete_product" android:icon="@drawable/sym_action_add" android:title="delete" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_modify_product" android:icon="@drawable/sym_action_add" android:title="modify" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_product_details" android:icon="@drawable/sym_action_add" android:title="details" android:showAsAction="ifRoom|withText" />
If you're willing to be a bit adventurous, look at Google's source code for PopupMenu. Create your own class i.e. MyPopupMenu that is the same as Google's PopupMenu class, but make one slight change.
In PopupMenu's constructor:
public MyPopupMenu(Context context, View anchor) { // TODO Theme? mContext = context; mMenu = new MenuBuilder(context); mMenu.setCallback(this); mAnchor = anchor; mPopup = new MenuPopupHelper(context, mMenu, anchor); mPopup.setCallback(this); mPopup.setForceShowIcon(true); //ADD THIS LINE }
use the method setForceShowIcon to force it to show the icon. You can also just expose a public method to set this flag as well depending on your needs.
Contribution to the solution provided by Gaelan Bolger. Use this code if you get a "IllegalAccessException: access to field not allowed".
PopupMenu popup = new PopupMenu(mContext, view); try { Field[] fields = popup.getClass().getDeclaredFields(); for (Field field : fields) { if ("mPopup".equals(field.getName())) { field.setAccessible(true); Object menuPopupHelper = field.get(popup); Class<?> classPopupHelper = Class.forName(menuPopupHelper .getClass().getName()); Method setForceIcons = classPopupHelper.getMethod( "setForceShowIcon", boolean.class); setForceIcons.invoke(menuPopupHelper, true); break; } } } catch (Exception e) { e.printStackTrace(); } prepareMenu(popup.getMenu()); popup.show();
I was able to show the icons using reflection. It may not be the most elegant solution but it works.
try { Class<?> classPopupMenu = Class.forName(popupMenu .getClass().getName()); Object menuPopupHelper = classPopupMenu.getDeclaredField( "mPopup").get(popupMenu); Class<?> classPopupHelper = Class.forName(menuPopupHelper .getClass().getName()); Method setForceIcons = classPopupHelper.getMethod( "setForceShowIcon", boolean.class); setForceIcons.invoke(menuPopupHelper, true); } catch (Exception e) { e.printStackTrace(); }
http://stackoverflow.com/questions/6805756/is-it-possible-to-display-icons-in-a-popupmenu
相关文章推荐
- Is it possible to display icons in a PopupMenu?PopupMenu中显示图片是否可能
- It is not possible to run two different versions of ASP.NET in the same IIS process:IIS
- It is not possible to run two different versions of ASP.NET in the same IIS process.问题的解决
- IELTS12 Test5 It is good to share as much information as possible in any areas
- It is not possible to run two different versions of ASP.NET in the same IIS process
- Is it possible to show some Word Document (or Excel document) in a UniGUI Frame ?
- Is it possible to implement IPv6 on a virtual interface (eth0:1 interface) in Linux?
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- 323. Nothing in the world is difficult for one who sets his mind to it. 世上无难事,只怕有心人
- Sharepoint Error : The URL "filename" is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
- It is said that wars in the 21st century will be fought over water. Do you agree? What do you think can be done now to alleviate
- Is it possible to change A Menu Item's Type?
- 学语言最好的方法是尽量多地联系使用它。(The way to learn a language is to practice using it as often as possible.)
- How to scroll an image, if it does not fit in the display
- How to get a type in C++ when its template argument is the argument
- here is aprogram for doing it in the mobile to mobile
- What is reflection and how to use it in CSharp ?
- Is it possible to set font for entire Application?
- c++ - In STL maps, is it better to use map::insert than []? - Stack Overflow
- this path is in ef to find it,if i have enough time i will to visite this website again