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

}
4

3 回答 3

94

你需要打电话setHasOptionsMenu(true)进来onCreate()

为了向后兼容,最好将此调用尽可能晚地放置在末尾onCreate()或什至稍后onActivityCreated()或类似的地方。

请参阅:https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

于 2013-09-10T08:58:50.533 回答
90

我迟到了答案,但我认为这是另一种解决方案,此处未提及,因此发布。

第 1 步:制作一个要添加的菜单 xml,就像我必须在我的操作栏上添加一个过滤器操作一样,所以我创建了一个 xml filter.xml。要注意的主线是android:orderInCategory这将在您想要显示的任何地方首先或最后显示操作图标。还有一点要注意的是值,如果值较小,则首先显示,如果值较大,则最后显示。

过滤器.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()方法中,只需将下面的行放在上面,它负责回调onCreateOptionsMenu(Menu menu, MenuInflater inflater)方法,就像在Activity中一样。

@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方法,当您从actionBar选择添加的操作图标时,您可以通过该方法实现任何您想做的逻辑:

@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);
    }
于 2015-08-11T07:38:32.610 回答
-18

在 AndroidManifest.xml 中设置主题全息是这样的:

<activity
android:name="your Fragment or activity"
android:label="@string/xxxxxx"
android:theme="@android:style/Theme.Holo" >

于 2015-05-10T18:54:18.983 回答