以编程方式设置 PopupMenu 菜单项 [英] Setting PopupMenu menu items programmatically
本文介绍了以编程方式设置 PopupMenu 菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 PopupMenu
并且我知道将菜单与其关联的常用方法是使用 popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu());
或类似的东西.我的问题是,我在菜单中有一组我想要的项目,我需要能够在 Java 中以编程方式更改它们.我怎样才能做到这一点?
I have a PopupMenu
and I know the usual way to associate a menu to it is to use popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu());
or something of the like. My problem is, I have an array of items that I want in the menu and I need to be able to change them programmatically in Java. How can I do this?
推荐答案
刚想通;对于任何遇到同样问题的人,您只需这样做:
Just figured it out; for anyone who runs into this same problem you just do:
popup.getMenu().add(groupId, itemId, order, title);
对于您要添加的每个 MenuItem
.
for each MenuItem
you want to add.
这篇关于以编程方式设置 PopupMenu 菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文