我在ActionBar中有选项菜单A的FragmentActivity,在此Activity中,我将一个Fragment和ActionBar更改添加到新选项菜单B.但添加此片段后,菜单A和B都显示在ActionBar中。如何在片段可见时删除菜单A?
答案 0 :(得分:0)
我现在有了答案。 在FragmentActivity中我有
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_page_menu, menu);
menu.setGroupVisible(R.id.menu_group_main, true); // this line to show menu of Activity
return super.onCreateOptionsMenu(menu);
}
用
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/menu_group_main" >
<item
android:id="@+id/item_grid"
android:icon="@drawable/icon_grid"
android:showAsAction="always"
android:title="@string/action_grid"/>
</group>
片段我将设置
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_grid_thumb, menu);
menu.setGroupVisible(R.id.menu_grid_thumb, true);//this line to show menu of Fragment
menu.setGroupVisible(R.id.menu_group_main, false);// this line to hide menu of Activity
super.onCreateOptionsMenu(menu, inflater);
}
用
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/menu_grid_thumb" >
<item
android:id="@+id/item_frag_grid"
android:icon="@drawable/icon_grid"
android:showAsAction="always"
android:title="@string/action_grid"/>
</group>
运行良好