gpt4 book ai didi

android - PopupMenu 图标不显示

转载 作者:行者123 更新时间:2023-12-02 05:32:40 25 4
gpt4 key购买 nike

我创建了一个 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;
}
}

预先感谢您的帮助。

最佳答案

我在这个 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();
}

关于android - PopupMenu 图标不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com