静态选项菜单 [英] Static options menu
问题描述
我要创造我所有的活动画面的静态选项菜单。我不希望重写 onCreateOptionsMenu()
中的每一项活动。
I want to create a static options menu for all my activity screens. I dont want to override onCreateOptionsMenu()
in each activity.
由于菜单
类是一个庞大的数字的方法,其难度创建实现类的静态对象。
Since Menu
class is an interface with a huge number of methods, its difficult to create a static object of the implementing class.
做同样的任何其他方式?
Any other way of doing the same?
推荐答案
如果我看了你的问题正确,你想要的一样的菜单中的所有活动。我能想到的两种方法可以做到这一点:
If I read your question correctly you want the same menu in all your Activities. I can think of two ways to do this:
-
创建
活动的一个子类
实现onCreateOptionsMenu()
和onOptionsItemSelected ()
(也可能是在prepareOptionsMenu
)。然后让你的所有活动
类扩展这个子类。
Create a subclass of
Activity
that implementsonCreateOptionsMenu()
andonOptionsItemSelected()
(and possiblyonPrepareOptionsMenu
). Then have all yourActivity
classes extend this subclass.
创建一个静态方法的地方被称为像 populateOptionsMenu()
,需要一个菜单
(也可能是一个上下文
)作为参数。你的活动
类就可以调用从 onCreateOptionsMenu()
方法,这种填充菜单
。还就需要相应的 processItemSelected()
静态方法,当项目被点击了。
Create a static method somewhere called something like populateOptionsMenu()
that takes a Menu
(and probably a Context
) as arguments. Your Activity
classes can then call this from their onCreateOptionsMenu()
methods to populate the Menu
. You'd also need a corresponding processItemSelected()
static method for when items were clicked.
选项1似乎最好的,因为它不会需要同样bolierplate你们的活动来调用静态方法。
Option 1 seems best as it wouldn't require the same bolierplate in your Activities to call the static methods.
这篇关于静态选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!