Android:跨多个活动显示菜单
我的应用程序中有多项活动。我希望我的活动具有相同的菜单项,除了一个。我想知道是否有一种简单的方法可以将菜单项放入所有选定的活动中,而不是为每个活动复制并粘贴菜单项?基本上我想与选定的活动共享菜单项。
谢谢,
I have several activities in my app. I want my activities to have the same menuitem, except one. I was wondering if there is an easy way to put the menuitem in all selected activities instead of copying and pasting the menuitem for each single activity? Basically I want to share the menuitem with selected activities.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个扩展 Activity 的类(例如 A 类),然后通过扩展此类来创建所有其他活动。现在,在 A 中,将常用菜单项存储在向量中,并且可以为任何特定活动添加任何特定的菜单操作。这样您就可以避免在每个活动中复制和粘贴菜单项的麻烦
create a class (say class A) which extends activity and then create all your other activities by extending this class. Now in A keep the common menu items stored in a vector and any specific menu action can be added for any particular activity. This way you avoid the trouble of copying and pasting the menu items in every activity
这是 MVC 背后的总体思想 - 能够使用相同的模型(菜单数据)显示不同的视图(活动)。
如果动态加载这些菜单 - 使用菜单数据创建一个 Singleton 类并从中获取它。
如果这些菜单已在 xxx.xml 中定义 - 您必须创建新的 menu.xml(例如)并在其中提取菜单描述。然后将其包含在需要该菜单的所有活动布局中。
您还可以随时动态扩充该 menu.xml。
This is the general idea behind MVC - to be able to show different Views (Activities) with same model(menu data).
If you load these menus dynamically - make a Singleton class with the menu data and get it from it.
If these menus are already defined in xxx.xml - you must create new menu.xml (for example) and extract the menu description there. Then include it in all activity layouts which need that menu.
You can also always dynamicaly inflate that menu.xml.