PopupMenu 图标不显示
PopupMenu Icon doesn't show
我创建了一个 ListView
,每个项目都有一个 PopupMenu
。我创建了一个 menu layout
并将其用作我的 PopupMenu
。我的问题是每次单击 ListView
项目中的省略号选项时,PopupMenu
都会显示 Text
,但 Icon
不会显示。
这是我的 xml 菜单:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_retry"
android:icon="@drawable/retry"
android:title="@string/retry"
app:showAsAction="always"
/>
<item
android:id="@+id/action_delete"
android:icon="@drawable/delete"
android:title="@string/delete"
app:showAsAction="always"
/>
</menu>
然后在我的 Adapter
中,我的 ListView
是这样的:
public class MyListViewAdapter extends BaseAdapter implements MenuItem.OnMenuItemClickListener {
.....
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.mylistrow, null);
}
final View action = (View) convertView.findViewById(R.id.action);
action.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
showPopup(action, position);
}
});
// ....codes for listview creation....
return convertView;
}
public void showPopup(View v, int listItemPosition) {
PopupMenu popup = new PopupMenu(mContext, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.outbox_menu, popup.getMenu());
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_retry:
return true;
case R.id.action_delete:
return true;
default:
return false;
}
}
提前感谢您的帮助。
MenuBuilder
是一个隐藏的 class 但包含显示图标的方法。您将需要使用反射来显示菜单中的图标。尝试在 showPopop(View, int)
:
中添加
PopupMenu popup = new PopupMenu(mContext, v);
try {
Method method = popup.getMenu().getClass().getDeclaredMethod("setOptionalIconsVisible", boolean.class);
method.setAccessible(true);
method.invoke(popup.getMenu(), true);
} catch (Exception e) {
e.printStackTrace();
}
我在这个 link 中找到了解决方案并将其应用到我的代码中。
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();
}
在 Kotlin 中
val popup = PopupMenu(context, holder.img_menu)
try {
popup.inflate(R.menu.bank_edit_menu)
val fields: Array<Field> = popup.javaClass.declaredFields
for (field in fields) {
if ("mPopup" == field.getName()) {
field.setAccessible(true)
val menuPopupHelper: Any = field.get(popup)
val classPopupHelper =
Class.forName(menuPopupHelper.javaClass.name)
val setForceIcons: Method = classPopupHelper.getMethod(
"setForceShowIcon",
Boolean::class.javaPrimitiveType
)
setForceIcons.invoke(menuPopupHelper, true)
break
}
}
popup.setOnMenuItemClickListener(object : PopupMenu.OnMenuItemClickListener{
override fun onMenuItemClick(p0: MenuItem?): Boolean {
Log.e(">>",p0.toString())
return true
}
})
popup.show();
} catch (e: Exception) {
e.printStackTrace()
}
我创建了一个 ListView
,每个项目都有一个 PopupMenu
。我创建了一个 menu layout
并将其用作我的 PopupMenu
。我的问题是每次单击 ListView
项目中的省略号选项时,PopupMenu
都会显示 Text
,但 Icon
不会显示。
这是我的 xml 菜单:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_retry"
android:icon="@drawable/retry"
android:title="@string/retry"
app:showAsAction="always"
/>
<item
android:id="@+id/action_delete"
android:icon="@drawable/delete"
android:title="@string/delete"
app:showAsAction="always"
/>
</menu>
然后在我的 Adapter
中,我的 ListView
是这样的:
public class MyListViewAdapter extends BaseAdapter implements MenuItem.OnMenuItemClickListener {
.....
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.mylistrow, null);
}
final View action = (View) convertView.findViewById(R.id.action);
action.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
showPopup(action, position);
}
});
// ....codes for listview creation....
return convertView;
}
public void showPopup(View v, int listItemPosition) {
PopupMenu popup = new PopupMenu(mContext, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.outbox_menu, popup.getMenu());
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_retry:
return true;
case R.id.action_delete:
return true;
default:
return false;
}
}
提前感谢您的帮助。
MenuBuilder
是一个隐藏的 class 但包含显示图标的方法。您将需要使用反射来显示菜单中的图标。尝试在 showPopop(View, int)
:
PopupMenu popup = new PopupMenu(mContext, v);
try {
Method method = popup.getMenu().getClass().getDeclaredMethod("setOptionalIconsVisible", boolean.class);
method.setAccessible(true);
method.invoke(popup.getMenu(), true);
} catch (Exception e) {
e.printStackTrace();
}
我在这个 link 中找到了解决方案并将其应用到我的代码中。
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();
}
在 Kotlin 中
val popup = PopupMenu(context, holder.img_menu)
try {
popup.inflate(R.menu.bank_edit_menu)
val fields: Array<Field> = popup.javaClass.declaredFields
for (field in fields) {
if ("mPopup" == field.getName()) {
field.setAccessible(true)
val menuPopupHelper: Any = field.get(popup)
val classPopupHelper =
Class.forName(menuPopupHelper.javaClass.name)
val setForceIcons: Method = classPopupHelper.getMethod(
"setForceShowIcon",
Boolean::class.javaPrimitiveType
)
setForceIcons.invoke(menuPopupHelper, true)
break
}
}
popup.setOnMenuItemClickListener(object : PopupMenu.OnMenuItemClickListener{
override fun onMenuItemClick(p0: MenuItem?): Boolean {
Log.e(">>",p0.toString())
return true
}
})
popup.show();
} catch (e: Exception) {
e.printStackTrace()
}