我使用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
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
android:id="@+id/notification"
android:icon="@drawable/ic_action_notification"
android:title="@string/mnuNotificationText"
app:showAsAction="always"/>
它是根本不显示在任何片段中还是仅在某些情况下(未更新等)显示?
并非所有带有导航抽屉的片段。
请检查此代码段
public class Company1 extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_company_profile,container, false);
setHasOptionsMenu(true);
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);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
//You can change menuitem property
//menu.findItem(R.id.notification).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
}