弹出菜单没有膨胀
Popup menu is not inflating
如何使用 OnMenuItemClickListeners 创建自定义弹出菜单 class?我似乎找不到任何使用单独 class 的答案。他们都在他们的活动中这样做,但我想要一个单独的弹出菜单 class。
我试过创建它:
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();
假设您有一个按钮,单击该按钮将显示弹出菜单。按照此操作来扩充自定义弹出菜单。
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
}
});
请尝试以下代码。
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 并将其从自定义 class
中删除
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;
}
};
希望对您有所帮助。
------------------------ 快乐编码-------------------- -
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu);
// Need to add this line otherwise it won't work.
addPhotoMenu.show();
感谢:
如何使用 OnMenuItemClickListeners 创建自定义弹出菜单 class?我似乎找不到任何使用单独 class 的答案。他们都在他们的活动中这样做,但我想要一个单独的弹出菜单 class。
我试过创建它:
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();
假设您有一个按钮,单击该按钮将显示弹出菜单。按照此操作来扩充自定义弹出菜单。
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
}
});
请尝试以下代码。
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 并将其从自定义 class
中删除 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;
}
};
希望对您有所帮助。
------------------------ 快乐编码-------------------- -
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu);
// Need to add this line otherwise it won't work.
addPhotoMenu.show();
感谢: