带有图标的弹出菜单
PopUp menu with icons
我是 android 的新手。我无法在弹出菜单中添加简单的图标。
我将图标添加到 xml 文件,但我看不到它们。
这是我的菜单 - pop_up_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_email"
android:icon="@mipmap/email"
android:title="E-Mail"
/>
<item
android:id="@+id/action_messenger"
android:icon="@mipmap/messenger"
android:title="Messenger"
/>
<item
android:id="@+id/action_skype"
android:icon="@mipmap/skype"
android:title="Skype"
/>
<item
android:id="@+id/action_whatsapp"
android:icon="@mipmap/whatsapp"
android:title="Whatsapp"
/>
</menu>
我有这个 OnClickListener,它在单击 moreActionsButton 后调用 showPopUpMenu 函数
moreActionsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopUpMenu(us);
}
});
这是创建菜单的 showPopUpMenu
public void showPopUpMenu(final User user) {
View menuItemView = getView().findViewById(R.id.groupLeave);
PopupMenu popUpMenu = new PopupMenu(getActivity(), menuItemView);
popUpMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popUpMenu.getMenu());
popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_messenger:
onMessengerClick(user);
break;
case R.id.action_skype:
onSkypeClick(user);
break;
case R.id.action_whatsapp:
onWhatsappClick(user);
break;
case R.id.action_email:
onEmailClick(user);
break;
default:
break;
}
return true;
}
});
popUpMenu.show();
}
默认情况下,PopupMenu 不会在您的标题旁边显示图标。
您需要做的是创建自己的 PopupMenu 实现并通过覆盖默认构造函数强制使用图标:
public CustomPopupMenu(Context context, View anchor) {
...
mPopup.setForceShowIcon(true);
}
或
在显示 PopupMenu 之前,使用反射调用所需的方法
//... initialization of your PopupMenu
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popupMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and NoSuchFieldError
//
// In either case, an exception indicates something is wrong with the reflection code, or the
// structure of the PopupMenu class or its dependencies has changed.
//
// These exceptions should never happen since we're shipping the AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to display, so log the error and
// show the menu normally.
}
popUpMenu.show();
我是 android 的新手。我无法在弹出菜单中添加简单的图标。 我将图标添加到 xml 文件,但我看不到它们。
这是我的菜单 - pop_up_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_email"
android:icon="@mipmap/email"
android:title="E-Mail"
/>
<item
android:id="@+id/action_messenger"
android:icon="@mipmap/messenger"
android:title="Messenger"
/>
<item
android:id="@+id/action_skype"
android:icon="@mipmap/skype"
android:title="Skype"
/>
<item
android:id="@+id/action_whatsapp"
android:icon="@mipmap/whatsapp"
android:title="Whatsapp"
/>
</menu>
我有这个 OnClickListener,它在单击 moreActionsButton 后调用 showPopUpMenu 函数
moreActionsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopUpMenu(us);
}
});
这是创建菜单的 showPopUpMenu
public void showPopUpMenu(final User user) {
View menuItemView = getView().findViewById(R.id.groupLeave);
PopupMenu popUpMenu = new PopupMenu(getActivity(), menuItemView);
popUpMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popUpMenu.getMenu());
popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_messenger:
onMessengerClick(user);
break;
case R.id.action_skype:
onSkypeClick(user);
break;
case R.id.action_whatsapp:
onWhatsappClick(user);
break;
case R.id.action_email:
onEmailClick(user);
break;
default:
break;
}
return true;
}
});
popUpMenu.show();
}
默认情况下,PopupMenu 不会在您的标题旁边显示图标。
您需要做的是创建自己的 PopupMenu 实现并通过覆盖默认构造函数强制使用图标:
public CustomPopupMenu(Context context, View anchor) {
...
mPopup.setForceShowIcon(true);
}
或
在显示 PopupMenu 之前,使用反射调用所需的方法
//... initialization of your PopupMenu
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popupMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and NoSuchFieldError
//
// In either case, an exception indicates something is wrong with the reflection code, or the
// structure of the PopupMenu class or its dependencies has changed.
//
// These exceptions should never happen since we're shipping the AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to display, so log the error and
// show the menu normally.
}
popUpMenu.show();