问题描述
我真的很喜欢新的PopupMenu我们得到了3.0,但我就是无法显示任何图标旁边,它的菜单项。我膨胀的菜单从以下的.xml:
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:
<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" />
通过这个code:
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu pop = new PopupMenu(getActivity(), v);
pop.getMenuInflater().inflate(R.menu.shelves_details_menu, pop.getMenu());
pop.show();
}
});
我不能得到的图标显示出来,我失去了一些东西?
I can't get the icons to show up, am I missing something?
感谢
推荐答案
如果你愿意成为一个有点冒险,看看谷歌的源头code的弹出菜单。创建你自己的类,即MyPopupMenu是一样的谷歌的PopupMenu的类,但做一个细微的变化。
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.
在PopupMenu的构造函数:
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
}
使用的方法setForceShowIcon,迫使它显示的图标。你也可以公开公共方法来设置该标志,以及根据您的需求。
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.
这篇关于是否有可能显示一个PopupMenu的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!