如何使用OnMenuItemClickListeners创建自定义弹出菜单类?我似乎找不到任何使用单独类的答案。他们在活动中都这样做,但是我想要一个单独的弹出菜单类。
我尝试创建它:
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();
答案 0 :(得分:0)
假设您有一个按钮,然后单击该按钮,将显示弹出菜单。请遵循此步骤以添加自定义弹出菜单。
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
popup.getMenuInflater().inflate(R.menu.popup_menu_fragevent_addphotos, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
return true;
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
return true;
}
return false;
}
});
popup.show();//showing popup menu
}
});
答案 1 :(得分:0)
请尝试以下代码。
public class AddPhotoMenu extends PopupMenu {
public AddPhotoMenu(Context context, View anchor) {
super(context, anchor);
getMenuInflater().inflate(R.menu.popup_menu_fragevent_addphotos, getMenu());
}
@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;
}
};
}
尝试从构造函数中填充菜单布局,而无需覆盖inflate方法。
您可以覆盖Activity / Fragment中的setOnMenuItemClickListener并将其从自定义类中删除
addPhotoMenu .setOnMenuItemClickListener(new PopupMenu.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;
}
};
我希望这会对您有所帮助。
------------------------快乐编码-------------------- < / p>
答案 2 :(得分:0)
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu);
// Need to add this line otherwise it won't work.
addPhotoMenu.show();
信用至: