Android 中所有活动中的相同选项菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3270206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Same option menu in all Activities in Android
提问by sunil
I have 10-15 activities in my project. I want to have the option menu mostly in all Activities. Then is their any way we can do it at one place and it appears in all activities.
我的项目中有 10-15 个活动。我希望在所有活动中都有选项菜单。然后是他们的任何方式,我们可以在一个地方做到这一点,它出现在所有活动中。
Also, I will like to hide the option menu in some. So, is it possible or I have to write option menu code in all activities.
另外,我想隐藏一些选项菜单。那么,是否有可能或者我必须在所有活动中编写选项菜单代码。
Regards
问候
Sunil
苏尼尔
回答by st0le
Create a Class (say BaseActivity) that extends Activity, and override onCreateOptionsMenu
and onOptionsItemSelected
functions.
创建一个扩展 Activity的类(比如BaseActivity),并覆盖onCreateOptionsMenu
和onOptionsItemSelected
函数。
public class BaseActivity extends Activity {
// Activity code here
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// do what you want here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Now, in the other 15-16 activities, instead of extending an Activity, you should extend BaseActivity.
现在,在其他 15-16 个活动中,您应该扩展BaseActivity而不是扩展 Activity 。
public class FooActivity extends BaseActivity {
// Activity code here
}
This way, all your activities derive the options menu. For activities where you want the options menu disabled, you can override it again in that particular activity.
这样,您的所有活动都会派生出选项菜单。对于您希望禁用选项菜单的活动,您可以在该特定活动中再次覆盖它。
public class BarActivity extends BaseActivity {
// Activity code here
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Do Nothing
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Do Nothing
}
}
Hopefully, it doesn't give you problems in the manifest file.
希望它不会在清单文件中给您带来问题。
回答by Madhvi Mamtora
The solution to this problem is in your new activity add this menu method.
此问题的解决方案是在您的新活动中添加此菜单方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home_page, menu);
menu.removeItem(R.id.logout);
return true;
}
In each activity this method will automatically appear.
在每个活动中,这个方法都会自动出现。
If it doesn't then add it with the inflate
call. It requires two parameters, an xml resource(the same one that you used in your original activity), and the menu object that is pass into the onCreateOptionsMenu
method.
如果没有,则在inflate
调用中添加它。它需要两个参数,一个 xml 资源(与您在原始活动中使用的相同)和传递到onCreateOptionsMenu
方法中的菜单对象。
menu.removeItem
will remove the menu item of whatever resource id you pass to it.
I hope this helps those who are facing this problem.
menu.removeItem
将删除您传递给它的任何资源 ID 的菜单项。我希望这可以帮助那些面临这个问题的人。
Thank you, and happy to share this post.
谢谢你,很高兴分享这篇文章。
回答by Ojonugwa Jude Ochalifu
It is not enough to just extend the BaseActivity
, you must also call super.onCreateOptionsMenu(menu)
and super.onOptionsItemSelected(item)
like this in your other activities:
仅扩展 是不够的BaseActivity
,您还必须在其他活动中调用super.onCreateOptionsMenu(menu)
并super.onOptionsItemSelected(item)
像这样:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//getMenuInflater().inflate(R.menu.menu_second, menu); <- remove this
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}