我正在使用常用的抽屉导航 Activity 来托管一系列 fragment 。当我从抽屉导航中更改为不同的 fragment 时,我试图实现单独的 ActionBar 按钮。
我遇到了一个问题,即项目没有直接添加到操作栏,而是作为下拉选择添加到“三点”溢出菜单。
操作栏上的按钮不可用,我做错了什么?
工作 fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_jobs, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
//inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.job_navigation, menu);
super.onCreateOptionsMenu(menu, inflater);
}
菜单 XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_drawer"
android:showAsAction="ifRoom"
android:title="@string/action_search"/>
</menu>
最佳答案
您应该将 setHasOptionsMenu(true) 移动到 fragment 的 onCreate() 方法
关于android - 操作栏按钮未显示在 fragment 更改中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282872/