片段中的菜单未显示 [英] Menu in Fragments not showing

查看:37
本文介绍了片段中的菜单未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

this.setHasOptionsMenu(true);

onCreateOptionsMenu 方法

the onCreateOptionsMenu method

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

xml 文件如下所示:

The xml file looks like this :

<?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);

onCreateOptionsMenu 方法

the onCreateOptionsMenu method

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

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>

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

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.

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

推荐答案

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

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

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

}

这篇关于片段中的菜单未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆