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

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

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

为了向后兼容,最好尽可能在onCreate()的末尾放置这个调用,或者在onActivityCreated()或其他类似的东西之后。

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

我迟到了,但我认为这是另一个解决scheme,这里没有提到这么张贴。

第1步:制作一个xml的菜单,你想添加,就像我必须在我的操作栏上添加一个filter动作,所以我创build了一个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> 

第二步:onCreate()方法的片段中,就像上面提到的那样,就像在一个Activity中一样,负责调用onCreateOptionsMenu(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方法,您可以通过该方法实现逻辑,无论您想从actionBar中select添加的操作图标,

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

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

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