我正在尝试在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);
}
}
}
发布于 2013-09-10 16:58:51
您需要在onCreate()
中调用setHasOptionsMenu(true)
。
为了向后兼容,最好在onCreate()
的末尾,甚至在onActivityCreated()
或类似的东西中,尽可能晚地进行此调用。
请参阅:https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)
发布于 2015-08-11 15:38:33
我回答晚了,但我认为这是另一个解决方案,这里没有提到,所以张贴。
步骤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>
Step 2: In onCreate() method of fragment只需像前面提到的那样放入以下行,它负责回调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-05-11 02:54:19
在AndroidManifest.xml中设置主题全息如下:
<activity
android:name="your Fragment or activity"
android:label="@string/xxxxxx"
android:theme="@android:style/Theme.Holo" >
https://stackoverflow.com/questions/18714322
复制相似问题