我想在 Android的碎片选项菜单。 动作条
选项菜单中没有我的片段显示。
下面是我的code和我都 onCreateOptionsMenu()
和 onOptionSelected()
功能。我的code不显示任何错误。但是,选项菜单不显示。
包org.reachout;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.LinearLayout;
进口org.general.R;
公共类ViewMessageFragment扩展片段{
/ *(非Javadoc中)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,android.view.ViewGroup,android.os.Bundle)
* /
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
如果(集装箱== NULL){
//我们有不同的布局,并在他们这一个
//片段的含框架不存在。片段
//仍然可以从它的保存的状态创建的,但是有
//没有理由去尝试创建视图,因为它的层次结构
//将不被显示。请注意,这是没有必要 - 我们可以
//只需运行code以下,我们将创建并返回
//视图层次;它只是永远不会被使用。
返回null;
}
返回(的LinearLayout)inflater.inflate(R.layout.viewmessages_tab_fragment_layout,集装箱,假);
}
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
// TODO自动生成方法存根
super.onCreateOptionsMenu(菜单,充气);
inflater.inflate(R.menu.askexperts_menu,菜单);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理项目选择
开关(item.getItemId()){
案例R.id.action_settings:
//做s.th.
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
}
解决方案
您需要调用 setHasOptionsMenu(真)
在的onCreate()
。
有关向后兼容性最好是尽可能晚地把这个调用在的onCreate()
,甚至后来在 onActivityCreated()或类似的东西。
请参阅:https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)
I am trying to have a options menu in Android Fragments. ActionBar
options menu are not displaying in my Fragments.
Here is my code and I have both onCreateOptionsMenu()
and onOptionSelected()
function. My code doesn't shows any error. But options menu are not displaying.
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);
}
}
}
解决方案
You need to call setHasOptionsMenu(true)
in onCreate()
.
For backwards compatibility it's better to place this call as late as possible at the end of onCreate()
or even later in onActivityCreated()
or something like that.
See: https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)