如何在Android Fragments中添加操作栏选项菜单

64

我正在尝试在 Android Fragments 中添加选项菜单。我的 ActionBar 选项菜单在片段中没有显示。

这是我的代码,我同时拥有 onCreateOptionsMenu()onOptionSelected() 函数。我的代码没有显示任何错误。但选项菜单没有显示。

package org.reachout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import org.general.R;

public class ViewMessageFragment extends Fragment {

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        return (LinearLayout)inflater.inflate(R.layout.viewmessages_tab_fragment_layout, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.askexperts_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       // handle item selection
       switch (item.getItemId()) {
          case R.id.action_settings:
             // do s.th.
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }   

}

1
它对我起作用了,我在onCreateView中使用了setHasOptionsMenu(true);而不是在onCreate中调用。从onCreate中调用setHasOptionsMenu(true);解决了我的问题。 - MTahir
与片段相关联的活动无关吗? 我的意思是,在活动中不必编写代码来显示菜单。 只需将片段附加到活动即可。 请尽快回复。 - sanidhya pal
3个回答

94

谢谢。但我已经在Android 4.0+上使其工作了。但是由于某种原因,在我的Android Gingerbread 2.3.3版本及以上版本上无法工作。我已经更新了我的代码。请帮助我在Android 2.3.3及以上版本中修复它。 - Karthik Sekar
3
setHasOptionsMenu(true) 移至 onCreateView() 方法的结尾。例如 View v = inflater.inflate(); setHasOptionsMenu(true); return v; - flx
我按照你说的尝试了,但在Android Gingerbread上仍然没有显示。 - Karthik Sekar
1
在4.4上,我开启了ABS,但它对我也不起作用。相反,我选择了另一条路线。我的片段在FragmentPagerAdapter中,我只是在onPageSelected()中动态添加和删除了一个菜单项。 - Eduard
1
在你的菜单项中设置'app:showAsAction="always"',@iDroidExplorer。 - Akshay Mahajan
显示剩余3条评论

89

虽然我回答晚了,但我认为这是另一种解决方案,因此在此发布。

步骤1:制作您想要添加的菜单的xml文件,例如我要在操作栏上添加一个筛选操作,因此我已创建了一个名为 filter.xml 的xml文件。要注意的主要内容是 android:orderInCategory ,这将在您想要显示的位置首先或最后显示操作图标。还有一件要注意的事情是值,如果值较小,则会显示在最前面,如果值较大,则会显示在最后面。

filter.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >


    <item
        android:id="@+id/action_filter"
        android:title="@string/filter"
        android:orderInCategory="10"
        android:icon="@drawable/filter"
        app:showAsAction="ifRoom" />


</menu>

步骤2:在碎片的onCreate()方法中,只需按照下面提到的方式添加以下代码行,该行代码负责调用与Activity中类似的 onCreateOptionsMenu(Menu menu, MenuInflater inflater) 方法。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

步骤 3:现在添加方法onCreateOptionsMenu,将其覆盖为:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.filter, menu);  // Use filter.xml from step 1
    }

步骤 4:现在添加 onOptionsItemSelected 方法,通过该方法您可以实现在从操作栏中选择添加的动作图标时执行的逻辑:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_filter){
            //Do whatever you want to do 
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

你的意思是在第三步中解压 filter.xml 吗?R.menu.search_result 是从哪里来的? - brianestey
是的,它是 filter.xml。在第三步中更改了 XML 的名称。 - Pankaj
我也这么想,但是没有弄清楚就不想修改你的回答,因为我还在慢慢地解决自己的问题。感谢你的更新。 - brianestey

-18
在 AndroidManifest.xml 中设置主题为 holo,如下所示:
<activity
android:name="your Fragment or activity"
android:label="@string/xxxxxx"
android:theme="@android:style/Theme.Holo" >


8
那样做确切地会如何有所帮助? - SagePawan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接