【发布时间】:2016-08-01 09:57:08
【问题描述】:
我的工具栏按钮没有显示在我尝试使用 setHasOptionsMenu(true); 的片段中。当我单击导航抽屉项目时,它会为每个片段加载新片段,我想显示不同的操作栏按钮(通知或保存)。以下是我的代码,在此先感谢。
以下是我的片段代码:(导航抽屉中的所有片段都相同)
public class Company1 extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_company_profile,container, false);
init(view);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDialog();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.company_profile, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.notification:
break;
}
return super.onOptionsItemSelected(item);
}
}
这是 company_profile.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="@+id/notification"
android:icon="@drawable/ic_action_notification"
android:title="@string/mnuNotificationText"
app:showAsAction="always"/>
</menu>
【问题讨论】:
-
它是完全没有出现在任何片段中还是仅在某些情况下(没有更新等)?
-
并非在所有带有导航抽屉的片段中。
标签: android android-fragments android-actionbar android-toolbar