简体   繁体   English

Android:SlidingMenu onCreateOptionsMenu 未在 Fragment 中调用

[英]Android: SlidingMenu onCreateOptionsMenu not called in Fragment

https://github.com/jfeinstein10/SlidingMenu https://github.com/jfeinstein10/SlidingMenu

Currently, I try to add new menu item in ColorFragment of library of SlidingMenu.目前,我尝试在 SlidingMenu 库的 ColorFragment 中添加新菜单项。

However, the method onCreateOptionsMenu did not get called.但是,没有调用onCreateOptionsMenu方法。

It's bug?是bug? Could you help to test add menu into to test class 'ColorFragment'??你能帮助测试将菜单添加到测试类“ColorFragment”中吗??

Android Version: 4.1.1安卓版本:4.1.1

Device: Tablet 7.0设备:平板7.0

public class ColorFragment extends Fragment {

private int mColorRes = -1;

public ColorFragment() {
    this(R.color.white);
}

public ColorFragment(int colorRes) {
    mColorRes = colorRes;
    setRetainInstance(true);


}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
 ..
 ..
 ..
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.menu_cust, menu);
}   

}

Just give it a try.. Hope it will help.试试吧..希望它会有所帮助。

set this in onCreateView()在 onCreateView() 中设置

setHasOptionsMenu(true);

and also change your onCreateOptionsMenu并更改您的 onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    menuInflater.inflate(R.menu.menu, menu);
}

I know it's quite old but it might help someone else.我知道它很旧,但它可能会帮助别人。

In addition to: (In onCreate/onCreateView)除了:(在onCreate/onCreateView中)

setHasOptionsMenu(true);

You should also set the action bar for the activity (In the fragment's onActivityCreated() method is a good place):您还应该为活动设置操作栏(在片段的 onActivityCreated() 方法中是个好地方):

//Kotlin
    (requireActivity() as AppCompatActivity).setSupportActionBar(mToolbar)
//Java
    ((AppCompatActivity) requireActivity()).setSupportActionBar(mToolbar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM