@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
Log.d(不,被调用);
inflater.inflate(R.menu.menuitem,菜单);
super.onCreateOptionsMenu(菜单,充气);
}
下面是我的onCreateView方法,在这里我打电话
@覆盖
公共查看onCreateView(LayoutInflater充气,
ViewGroup中的容器,包savedInstanceState){
setHasOptionsMenu(真正的);
返回inflater.inflate(R.layout.layout1,集装箱,假);
}
我不明白的日志报表或菜单获取调用我的行动吧。
更新:我试着从片段onCreate方法调用此,但菜单不显示
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(真正的);
}
menu.xml文件
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID /条的Android:标题=@字符串/节
机器人:图标=@可绘制/ ic_section
机器人:showAsAction =总是/>
<项目机器人:ID =@ + ID /刷新机器人:标题=@字符串/刷新
机器人:图标=@可绘制/ ic_refresh
机器人:showAsAction =总是/>
<项目机器人:ID =@ + ID / edit_patient机器人:标题=@字符串/ edit_patient
机器人:图标=@可绘制/ ic_editpatient
机器人:showAsAction =总是/>
<项目机器人:ID =@ + ID /关于机器人:标题=@字符串/约
机器人:showAsAction =从不/>
<项目机器人:ID =@ + ID /帮助机器人:标题=@字符串/帮助
机器人:showAsAction =从不/>
<项目机器人:ID =@ + ID / signout机器人:标题=@字符串/ signout
机器人:showAsAction =从不/>
< /菜单>
解决方案
您需要做的 setHasOptionsMenu(真);
从内部的片段的开始生命周期的方法之一。 pferably从$ P $在的onCreate(...)
。
在一个简约的情况下,的onCreate
您片段的方法是这样的:
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(真正的);
}
此外,调用 super.onCreateOptionsMenu(菜单,充气);
您已经膨胀后,您的自定义菜单将重置您刚才已经膨胀到一个空的菜单中的菜单
因此,无论电话:
@覆盖公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
Log.d(不,被调用);
super.onCreateOptionsMenu(菜单,充气);
inflater.inflate(R.menu.menuitem,菜单);
}
或
@覆盖公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
Log.d(不,被调用);
//没有超级调用
inflater.inflate(R.menu.menuitem,菜单);
}
另外,如果你正在测试一个姜饼设备上,菜单可能不会显示,如果托管活动
不包含它自己的菜单项。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
inflater.inflate(R.menu.menuitem, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Below is my onCreateView method, where i am calling
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.layout1, container, false);
}
I don't get the log statements or the menu getting called in my action-bar.
Update: I tried calling this from onCreate method of fragment, yet the menu is not shown.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/section" android:title="@string/section"
android:icon="@drawable/ic_section"
android:showAsAction="always" />
<item android:id="@+id/refresh" android:title="@string/refresh"
android:icon="@drawable/ic_refresh"
android:showAsAction="always" />
<item android:id="@+id/edit_patient" android:title="@string/edit_patient"
android:icon="@drawable/ic_editpatient"
android:showAsAction="always" />
<item android:id="@+id/about" android:title="@string/about"
android:showAsAction="never" />
<item android:id="@+id/help" android:title="@string/help"
android:showAsAction="never" />
<item android:id="@+id/signout" android:title="@string/signout"
android:showAsAction="never" />
</menu>
解决方案
You'll need to make a call of setHasOptionsMenu(true);
from within one of the starting lifecycle methods of the Fragment. Preferably from within onCreate(...)
.
In a minimalistic case the onCreate
method of your Fragment looks like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Also, calling super.onCreateOptionsMenu(menu, inflater);
after you have inflated your custom menu will reset the menu you just have inflated to an empty menu.
So either call:
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuitem, menu);
}
or:
@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
//no super call
inflater.inflate(R.menu.menuitem, menu);
}
Also, if you're testing on a Gingerbread device, the menu might not be displayed if the hosting Activity
does not contain a menu item of it's own.