android – 以编程方式删除弹出菜单项
作者:互联网
我正在我的应用程序中弹出一个弹出菜单.我已经创建了一个像下面这样的popmenu xml.
Song_popup xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ToolBarStyle">
<item
android:id="@+id/add_queue"
android:title="Add to queue" />
<item
android:id="@+id/play_next"
android:title="Add to favourite" />
<item
android:id="@+id/add_download"
android:title="Download" />
</menu>
现在我想通过检查条件来删除项目.我怎样才能做到这一点?
码
PopupMenu popup = new PopupMenu(activity, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.song_popup, popup.getMenu());
popup.show();
解决方法:
您可以删除菜单项,如下所示:
Menu m = popup.getMenu();
m.removeItem(m.findItem(R.id.add_queue)); //removes "Add to queue"
条件的实施完全取决于您.
这就是你可以处理菜单项点击的方法:
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if(menuItem.getItemId() == R.id.play_next){
Toast.makeText(YourActivity.this, "Play_next", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
标签:android,xml,popupmenu 来源: https://codeday.me/bug/20190716/1476263.html