gpt4 book ai didi

java - 弹出菜单没有膨胀

转载 作者:行者123 更新时间:2023-11-29 02:17:42 24 4
gpt4 key购买 nike

如何使用 OnMenuItemClickListeners 创建自定义弹出菜单类?我似乎找不到任何使用单独类的 SO 答案。他们都在他们的 Activity 中这样做,但我想要一个单独的弹出菜单类。

我试过创建它:

public class AddPhotoMenu extends PopupMenu {
public AddPhotoMenu(Context context, View anchor) {
super(context, anchor);
}

@Override
public void inflate(int menuRes) {
super.inflate(R.menu.popup_menu_fragevent_addphotos);
}

@Override
public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");

break;
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout

Log.d(TAG, "onMenuItemClick: Select photo");
break;

}
return false;
}
};
}

但是当我调用它时,菜单并没有展开。

 AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.show();

最佳答案

addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu);
// Need to add this line otherwise it won't work.
addPhotoMenu.show();

感谢:

PopupMenu onMenuItemClick not being called

关于java - 弹出菜单没有膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59279130/

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