在 Android 上实现应用程序范围选项菜单的最佳方法是什么?
我是一名 Android 新手,正在开发一个需要选项菜单的应用程序。我目前通过将选项菜单设置为主要活动并在主要活动中扩展它来实现选项菜单。
但由于我在团队中工作,这种方法并不总是适用于我们,因为我们需要扩展另一项重要的活动。
我的问题 如何在整个应用程序中实现此选项菜单而不扩展主活动中的活动?
我当前的设置
- 我有一个 MainActivity (首先启动) - MainActivity 扩展了 MenuClass
- 我有 OptionsMenu 类,MenuClass (我希望这是应用程序范围内的) - MenuClass 扩展了 Activity
- 我还有其他三个类,即扩展了 Activity 本身!这三个活动都是从MainActivity触发的,完成后返回到MainActivity。
I'm an Android newbie, and working on an app which needs an Options menu. I have currently implemented the options menu by setting it as the primary activity and Extending it in the main activity.
But since I work in a team, this method doesn't work always with us since we need to extend another activity which is essential.
My Question
How do I implement this Options Menu across the application without Extending the activity in my Main activity?
My Current Setup
- I have a MainActivity (This starts first) - MainActivity extends MenuClass
- I have the OptionsMenu Class, MenuClass (I want this to be Application wide) - MenuClass extends Activity
- I have three other Classes, that extends Activity itself! And these three activities are triggered from the MainActivity and when done, returns to the MainActivity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想或无法创建一个基础 Activity,然后每个其他 Activity 都会对其进行扩展 - 为什么您没有一个具有 public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 的实用程序类? ...} 函数和
public static boolean onOptionsItemSelected(MenuItem item) {...}
?然后从您的 Activity 中您可以执行以下操作:
您可能希望根据您将其构建到应用程序中的方式来更改其具体实现 - 即您可能不希望 utils 方法是静态的,因为您可能需要维护某些状态在那里,但这应该有效。
If you don't want to, or can't create a base Activity which every other activity then extends - why don't you have a utilities class which has a
public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {...}
function and apublic static boolean onOptionsItemSelected(MenuItem item) {...}
?then from you Activity you can do this:
You may want to change the exact implementation of this depending on how you build it in to your app - ie you may not want the utils methods to be static as you may require some state to be maintained in there, but this should work.