Android 片段中的菜单未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12090335/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:42:21  来源:igfitidea点击:

Menu in Fragments not showing

androidandroid-fragmentsandroid-menu

提问by El Duderino

I got pretty helpful hints to my last questions, I hope it won't be different this time :) First, let me explain the situation. I have an activity with 2 fragments. For each fragment I have a xml menu layout, the first has four menu entries, the second one has only one. Now at first the fragment with the four menu entries is loaded. In its onCreate method I set

我对上一个问题得到了非常有用的提示,我希望这次不会有所不同:) 首先,让我解释一下情况。我有一个包含 2 个片段的活动。对于每个片段,我都有一个 xml 菜单布局,第一个有四个菜单条目,第二个只有一个。现在首先加载带有四个菜单条目的片段。在我设置的 onCreate 方法中

this.setHasOptionsMenu(true);

the onCreateOptionsMenu method

onCreateOptionsMenu 方法

@Override 
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragshow, menu);
}

The xml file looks like this :

xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_refresh"
      android:icon="@drawable/ic_action_search"
      android:title="Refresh"
      android:visible="true"
      android:showAsAction="ifRoom|withText" />
<item android:id="@+id/menu_clearall"
      android:icon="@drawable/ic_action_search"
      android:title="Clear"
      android:visible="true"
      android:showAsAction="ifRoom|withText" />
<item android:id="@+id/menu_addcontent"
      android:icon="@drawable/ic_action_search"
      android:title="Add Content"
      android:visible="true"
      android:showAsAction="ifRoom|withText" />
<item android:id="@+id/menu_newlist"
      android:icon="@drawable/ic_action_search"
      android:title="New List"
      android:visible="true"
      android:showAsAction="ifRoom|withText" />    
</menu>

This works fine. All menu items are there, I can click them and respond. Now when I click on the "Add Content" menu, the other fragment is loaded. There I as well set

这工作正常。所有菜单项都在那里,我可以点击它们并做出回应。现在,当我单击“添加内容”菜单时,会加载另一个片段。在那里我也设置

this.setHasOptionsMenu(true);

the onCreateOptionsMenu method

onCreateOptionsMenu 方法

@Override 
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
    Log.i("FragCreateList","onCreateOptionsMenu called");
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragcreatelist, menu);
}

The xml file

xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_editdone"
      android:icon="@drawable/ic_action_search"
      android:title="Edit Done"
      android:visible="true"
      android:showAsAction="ifRoom|withText" />         
</menu>

But in the new fragment I can't see any menu items. Neither in the action bar, nor when I press the hardware menu button. In the former fragment, two menu items were in the actionbar, the remaining two appeared when the hardware button was pressed. But here nothing is shown. I get the logcat message that the onCreateOptionsMenu method got called, but nothing happens. (I even get a call to my (empty) onPrepareOptionsMenu method, which should be called right before the menu is shown, still nothing) Any clue what could be wrong? Calling invalidateOptionsMenu doesn't do a thing. Besides, I don't know for sure which one to call, as there is a getActivity().invalidateOptionsMenu and a getFragmentManager().invalidateOptionsMenu... Thanks in advance.

但是在新片段中我看不到任何菜单项。既不在操作栏中,也不在我按下硬件菜单按钮时。在前一个片段中,操作栏中有两个菜单项,其余两个在按下硬件按钮时出现。但这里什么也没有显示。我收到了 onCreateOptionsMenu 方法被调用的 logcat 消息,但没有任何反应。(我什至接到了对我的(空)onPrepareOptionsMenu 方法的调用,该方法应该在显示菜单之前立即调用,但仍然没有任何提示)任何线索可能是错误的?调用 invalidateOptionsMenu 没有任何作用。此外,我不确定要调用哪个,因为有一个 getActivity().invalidateOptionsMenu 和一个 getFragmentManager().invalidateOptionsMenu ......提前致谢。

Edit :

编辑 :

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

回答by Amir Alagic

I added setHasOptionsMenu(true)in onCreateView and it works fine

我在 onCreateView 中添加了setHasOptionsMenu(true)并且它工作正常

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.my_example_fragment, container, false);

}

回答by Abhinaw Sharma

Use menu.clear() before inflating menus.

在膨胀菜单之前使用 menu.clear()。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.mnulayout, menu);
}

回答by cubo1123

first of all your fragment has to have in the onCreate

首先你的片段必须在 onCreate

    setHasOptionsMenu(true);

and then you has to check that your activity DONT have the NoActionBar like theme its a little of tricky but if your activity has that theme doesn't matter where you call the setHasOptionsMenu(true)you fragment wont paint her

然后你必须检查你的活动是否有类似 NoActionBar 的主题它有点棘手但是如果你的活动有那个主题并不重要你在哪里调用setHasOptionsMenu(true)你的片段不会画她

回答by aglour

I had the same problem. I fixed it by using the showAsAction in the item applied to myApp context:

我有同样的问题。我通过在应用于 myApp 上下文的项目中使用 showAsAction 修复了它:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_refresh"
          android:icon="@drawable/ic_refresh"
          android:title="refresh"
          android:visible="true"
          myapp:showAsAction="ifRoom|withText"/>
</menu>

Sorry it is too late for you, but it may be useful for other people.

对不起,对你来说太晚了,但它可能对其他人有用。

回答by Orgazm pionerki

This happens because your device have hardware menu button. Therefore value of the variable "sHasPermanentMenuKey" is true in ViewConfiguration hasPermanentMenuKey(). You have to put the value of this variable false using reflection. Example:

发生这种情况是因为您的设备有硬件菜单按钮。因此,变量“sHasPermanentMenuKey”的值在 ViewConfiguration hasPermanentMenuKey() 中为真。您必须使用反射将此变量的值设为 false。例子:

// this method force showing menu button in device with hardware menu button
private void showOverflowMenu(FragmentActivity activity) {
    try {
        ViewConfiguration config = ViewConfiguration.get(activity);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

NOTE: use this method in onCreate() of activity not in method onCreate() of fragment.

注意:在活动的 onCreate() 中使用此方法,而不是在片段的 onCreate() 方法中。

回答by Cockpit Aliens

This actually works for me and could be your solution as well.

这实际上对我有用,也可能是您的解决方案。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

回答by jsmith

When registering for options/context menus in fragments, you generally need to wait until the onActivityCreated() call to ensure that there is an activity to tie the menu to. Try the fix suggested here and see if it helps:

在片段中注册选项/上下文菜单时,您通常需要等到 onActivityCreated() 调用以确保有一个活动可以将菜单绑定到。尝试此处建议的修复程序,看看是否有帮助:

Context menu from a ListFragment

ListFragment 的上下文菜单

回答by Michael Dovoh

Make sure you set setSupportActionBar(youtoolbar)if you use a Toolbarin your activity.

setSupportActionBar(youtoolbar)如果您Toolbar在活动中使用 a ,请确保设置。

回答by Vasil Li

An Options Menu may be hidden, but work. You can check this by long pressing the hardware button "Recent Apps".

选项菜单可能被隐藏,但有效。您可以通过长按硬件按钮“最近的应用程序”来检查这一点。

In addition to the mentioned reasons for the hidden menu, there may be a theme without an action bar specified in the AndroidManifest.xml

除了上面提到的隐藏菜单的原因外,可能还有AndroidManifest.xml中没有指定action bar的主题

    <application
        ...
        android:theme="@style/Theme.Design.NoActionBar"
        ...
    </application>

回答by ekenne chilex

For a fragment class in order for you to see your menu add sethasoptionsmenu(true) in either onCreateView() method or onCreateAcivity() method. But I added mine to onCreateView() method. Hope it helps someone

对于片段类,为了让您看到您的菜单,在 onCreateView() 方法或 onCreateAcivity() 方法中添加 sethasoptionsmenu(true)。但是我将我的添加到 onCreateView() 方法中。希望它可以帮助某人