儿童片段无法充气菜单 [英] Child Fragment Not able to inflate Menu
问题描述
你好我试图夸大或更改子片段菜单,但没能如愿。
流程是这样的。
如果我尝试从活动的膨胀菜单(主)---做工精细。
如果我尝试从infalte片段(SecondFrag)菜单----做工精细
如果我尝试从膨胀(SecondFrag)的子片段菜单----不工作
样code是这样
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(真);
Log.d(它具有不选项mener,+ hasOptionsMenu());
}
@覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
//super.onCreateOptionsMenu(menu,气筒);
Log.d(选项菜单,活动选项菜单);
。mActivity.getSupportMenuInflater()膨胀(R.menu.deal_detaisl,菜单);
Log.d(选项菜单,活动选项菜单);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){
案例R.id.deal_loc:
mActivity.showMapFragment();
打破; 案例R.id.deal_loc_route:
mActivity.showMapRoute();
打破;
}
返回false;
}
@覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
inflater.inflate(R.menu.deal_detaisl,菜单);
super.onCreateOptionsMenu(菜单,充气器);
}
这应该为你工作,试试吧。
编辑:
您应该创建一个自定义的片段
,使片段扩展的自定义片段
。然后在你的活动
为 onCreateOptionsMenu
就可以得到当前的片段
并呼吁充气菜单的公共方法(叫它什么)。聊天讨论后,您的问题得到解决。
Hi i am trying to inflate or change menu from child fragment , but not able to do so. The flow is like this .
If I try to inflate menu from activity (Main)--- work fine. if i try to infalte menu from fragment (SecondFrag)----work fine if i try to inflate menu from child fragment of(SecondFrag)----do not work
Sample Code is like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.d("It has option mener of not", ""+hasOptionsMenu());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//super.onCreateOptionsMenu(menu, inflater);
Log.d("Option Menu", "Activity Option Menu");
mActivity.getSupportMenuInflater().inflate(R.menu.deal_detaisl, menu);
Log.d("Option Menu", "Activity Option Menu");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.deal_loc:
mActivity.showMapFragment();
break;
case R.id.deal_loc_route:
mActivity.showMapRoute();
break;
}
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.deal_detaisl, menu);
super.onCreateOptionsMenu(menu, inflater);
}
This should work for you, try it.
Edit:
You should create a custom Fragment
and make the fragments extend that custom Fragment
. Then in your Activity
for onCreateOptionsMenu
you can get the current Fragment
and call a public method for inflating a menu (call it whatever). After a chat discussion, your problem was solved.
这篇关于儿童片段无法充气菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!