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

Kar*_*kar 61 android android-fragments android-optionsmenu android-actionbar

我想在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);
       }
    }   

}
Run Code Online (Sandbox Code Playgroud)

flx*_*flx 90

你需要调用setHasOptionsMenu(true)onCreate().

为了向后兼容,最好尽可能晚地拨打此电话,onCreate()或者甚至更晚onActivityCreated()或类似.

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

  • 在`onCreateView()`的末尾移动`setHasOptionsMenu(true)`.比如`View v = inflater.inflate(); setHasOptionsMenu(真); return v;` (3认同)

Pan*_*kaj 81

我迟到的答案,但我认为这是另一个解决方案,这里没有提到所以张贴.

第1步:创建一个要添加的xml菜单,就像我必须在操作栏上添加一个过滤器操作,这样我就创建了一个xml filter.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>
Run Code Online (Sandbox Code Playgroud)

第2步:在片段的onCreate()方法中,只需按下所述放入下面的行,它负责调用onCreateOptionsMenu(Menu菜单,MenuInflater inflater)方法,就像在Activity中一样.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
Run Code Online (Sandbox Code Playgroud)

第3步:现在添加onCreateOptionsMenu方法,它将覆盖为:

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

第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);
    }
Run Code Online (Sandbox Code Playgroud)