Android 隐藏/显示不同片段的操作栏选项菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23178663/
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
Hide/Show Action Bar Option Menu Item for different fragments
提问by Gaurav Arora
I have a Sherlock Fragment Activity in which there are 3 Fragments.
我有一个 Sherlock 片段活动,其中有 3 个片段。
Fragment A, Fragment B, Fragment C are three fragments. I want to show a done option menu in Fragment B only.
片段A、片段B、片段C是三个片段。我只想在片段 B 中显示完成的选项菜单。
And the activity is started with Fragment A. When Fragment B is selected done button is added.
并且活动以片段A启动。选择完成片段B后,添加了按钮。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(!menusInflated){
inflater.inflate(R.menu.security, menu);
menusInflated=true;
}
super.onCreateOptionsMenu(menu, inflater);
}
When I again start Fragment A I want to options Menu DONE (which was set at Fragment B) for this I am doing like this
当我再次启动 Fragment AI 时,我想为此选择 Menu DONE(在 Fragment B 中设置),我正在这样做
setHasOptionsMenu(false);
MenuItem item = (MenuItem) menu.findItem(R.id.done_item);
item.setVisible(false);
But this is not hiding at all, also it is giving NullPointerException when Activity if first started with Fragment A.
但这根本没有隐藏,如果 Activity 首次从 Fragment A 开始,它也会给出 NullPointerException。
Please let me know what is the problem.
请让我知道是什么问题。
回答by Silambarasan Poonguti
Try this...
尝试这个...
You don't need to override onCreateOptionsMenu() in your Fragment class again. Menu items visibility can be changed by overriding onPrepareOptionsMenu() method available in Fragment class.
您不需要再次覆盖 Fragment 类中的 onCreateOptionsMenu() 。可以通过覆盖 Fragment 类中可用的 onPrepareOptionsMenu() 方法来更改菜单项的可见性。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_search).setVisible(false);
super.onPrepareOptionsMenu(menu);
}
回答by C0D3LIC1OU5
This is one way of doing this:
这是这样做的一种方法:
add a "group" to your menu:
在菜单中添加一个“组”:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group
android:id="@+id/main_menu_group">
<item android:id="@+id/done_item"
android:title="..."
android:icon="..."
android:showAsAction="..."/>
</group>
</menu>
then, add a
然后,添加一个
Menu menu;
variable to your activity and set it in your override of onCreateOptionsMenu:
变量到您的活动并将其设置在您的 onCreateOptionsMenu 覆盖中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// inflate your menu here
}
After, add and use this function to your activity when you'd like to show/hide the menu:
之后,当您想显示/隐藏菜单时,将此功能添加到您的活动中并使用:
public void showOverflowMenu(boolean showMenu){
if(menu == null)
return;
menu.setGroupVisible(R.id.main_menu_group, showMenu);
}
I am not saying this is the best/only way, but it works well for me.
我并不是说这是最好的/唯一的方法,但它对我来说效果很好。
回答by Ojonugwa Jude Ochalifu
To show action items (action buttons) in the ActionBar of fragments where they are only needed, do this:
要在片段的 ActionBar 中仅需要它们的地方显示操作项(操作按钮),请执行以下操作:
Lets say you want the save
button to only show in the fragment where you accept input for items and not in the Fragment where you view a list of items, add this to the OnCreateOptionsMenu
method of the Fragment where you view the items:
假设您希望save
按钮仅显示在您接受项目输入的片段中,而不是显示在您查看项目列表的片段中,请将其添加到OnCreateOptionsMenu
您查看项目的片段的方法中:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (menu != null) {
menu.findItem(R.id.action_save_item).setVisible(false);
}
}
NOTE:For this to work, you need the onCreate()
method in your Fragment (where you want to hide item button, the item view fragment in our example) and add setHasOptionsMenu(true)
like this:
注意:为此,您需要onCreate()
Fragment 中的方法(您要隐藏项目按钮的位置,我们示例中的项目视图片段)并添加setHasOptionsMenu(true)
如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Might not be the best option, but it works and it's simple.
可能不是最好的选择,但它有效而且很简单。
回答by Pravinsingh Waghela
This will work for sure I guess...
我猜这肯定会起作用...
// Declare
Menu menu;
MenuItem menuDoneItem;
// Then in your onCreateOptionMenu() method write the following...
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
this.menu=menu;
inflater.inflate(R.menu.secutity, menu);
}
// In your onOptionItemSelected() method write the following...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.done_item:
this.menuDoneItem=item;
someOperation();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Now Making invisible any menu item...
public void menuInvisible(){
setHasOptionsMenu(true);// Take part in populating the action bar menu
menuDoneItem=(MenuItem)menu.findItem(R.id.done_item);
menuRefresh.setVisible(false); // make true to make the menu item visible.
}
//Use the above method whenever you need to make your menu item visible or invisiable
You can also refer this linkfor more details, it is a very useful one.
回答by Dhina k
MenuItem Import = menu.findItem(R.id.Import);
Import.setVisible(false)
For more reference, see: http://androiddhina.blogspot.in/2015/05/android-how-to-hide-menu-item-in.html
更多参考参考:http: //androiddhina.blogspot.in/2015/05/android-how-to-hide-menu-item-in.html
回答by Petrovich Denis
Try this
尝试这个
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.custom_actionbar, menu);
menu.setGroupVisible(...);
}
回答by Hantash Nadeem
By setting the Visibility of all items in Menu, the appbar menu or overflow menu will be Hide automatically
通过设置Menu中所有项目的Visibility,appbar菜单或溢出菜单将被自动隐藏
Example
例子
private Menu menu_change_language;
...
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
menu_change_language.findItem(R.id.menu_change_language).setVisible(true);
return super.onCreateOptionsMenu(menu);
}
Before going to other fragment use bellow code:
在转到其他片段之前,请使用以下代码:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
回答by Ibraheem Saoud
You can make a menu for each fragment, and a global variable that mark which fragment is in use now. and check the value of the variable in onCreateOptionsMenu and inflate the correct menu
您可以为每个片段制作一个菜单,以及一个标记当前正在使用哪个片段的全局变量。并检查 onCreateOptionsMenu 中变量的值并扩充正确的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (fragment_it == 6) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.custom_actionbar, menu);
}
}
回答by Suresh
Even though the question is old and answered. There is a simpler answer to that than the above mentioned. You don't need to use any other variables. You can create the buttons on action bar whatever the fragment you want, instead of doing the visibility stuff(show/hide).
即使这个问题很老而且得到了回答。有一个比上面提到的更简单的答案。您不需要使用任何其他变量。您可以在操作栏上创建您想要的任何片段的按钮,而不是执行可见性内容(显示/隐藏)。
Add the following in the fragment whatever u need the menu item.
无论您需要菜单项,都在片段中添加以下内容。
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Sample menu.xml file:
示例 menu.xml 文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_addFlat"
android:icon="@drawable/add"
android:showAsAction="ifRoom|withText"
android:title="@string/action_addFlat"/>
</menu>
Handling onclick events is as usual.
处理 onclick 事件与往常一样。
回答by Sandeep Sankla
Hello I got the best solution of this, suppose if u have to hide a particular item at on create Menu method and show that item in other fragment. I am taking an example of two menu item one is edit and other is delete. e.g menu xml is as given below:
您好,我得到了最好的解决方案,假设您必须在创建菜单方法中隐藏特定项目并在其他片段中显示该项目。我以两个菜单项为例,一个是编辑,另一个是删除。例如,菜单 xml 如下所示:
sell_menu.xml
销售菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_edit"
android:icon="@drawable/ic_edit_white_shadow_24dp"
app:showAsAction="always"
android:title="Edit" />
<item
android:id="@+id/action_delete"
android:icon="@drawable/ic_delete_white_shadow_24dp"
app:showAsAction="always"
android:title="Delete" />
Now Override the two method in your activity & make a field variable mMenu as:
现在在您的活动中覆盖这两个方法并将字段变量 mMenu 设置为:
private Menu mMenu; // field variable
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sell_menu, menu);
this.mMenu = menu;
menu.findItem(R.id.action_delete).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_delete) {
// do action
return true;
} else if (item.getItemId() == R.id.action_edit) {
// do action
return true;
}
return super.onOptionsItemSelected(item);
}
Make two following method in your Activity & call them from fragment to hide and show your menu item. These method are as:
在您的 Activity 中创建以下两个方法并从片段中调用它们以隐藏和显示您的菜单项。这些方法如下:
public void showDeleteImageOption(boolean status) {
if (menu != null) {
menu.findItem(R.id.action_delete).setVisible(status);
}
}
public void showEditImageOption(boolean status) {
if (menu != null) {
menu.findItem(R.id.action_edit).setVisible(status);
}
}
That's Solve from my side,I think this explanation will help you.
这是我这边的解决方案,我认为这个解释会对你有所帮助。