我喜欢在活动更改时更改菜单。我有 MainActivity.java 和 activity_main.xml 菜单。然后我有 MyDetailFragment.java 和 detail_view_menu.xml。MainActivity 菜单在 MainActivity.java 的 onCreateOptionsMenu 中膨胀为
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.activity_main, menu);
return (super.onCreateOptionsMenu(menu));
}
它工作正常。当点击其中一项并加载 MyDetailFragment.java 时,我喜欢显示 detail_view_menu.xml。我的实施方式是
public class MyDetailsFragment extends Fragment {
TrackerInfo trackerInfo;
public MyDetailsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_details_fragment, container, false);
setHasOptionsMenu(true);
TextView displayText = (TextView) view.findViewById(R.id.details);
displayText.setText(trackerInfo.getIdnumber());
return view;
}
public void retrieveData(TrackerInfo tracker){
trackerInfo = tracker;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_view_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.track:
return (true);
case R.id.view_history:
return (true);
}
return (super.onOptionsItemSelected(item));}}
但问题是菜单仍然是相同的activity_main 菜单并且从未改变过。可能是什么问题呢?