在工具栏上显示活动和片段的不同菜单选项 [英] Show different menu options on Toolbar for activity and fragment
问题描述
Android Studio 1.4
我有一个在activity_main.xml
中充气的工具栏.
我有一个名为main.xml
的菜单,该菜单会膨胀,上面只有一个图标可以显示.
I have a Toolbar that I inflate in my activity_main.xml
.
I have a menu called main.xml
that gets inflated that has just 1 icon to display on it.
当用户单击以打开片段时.我还有另一个带有2个图标的菜单friends.xml
.
When the user clicks to open a fragment. I have another menu friends.xml
that has 2 icons.
当我在片段中添加好友菜单时,它仍显示main.xml
菜单中的图标.
When I inflate the friends menu in the fragment it still displays the icon from the main.xml
menu.
我认为在工具栏上增加一个新菜单会删除现有菜单.
I thought that inflating a new menu on the toolbar would remove the existing menu.
这是main.xml菜单的屏幕截图.显示查找图标
This is a screenshot of the main.xml menu. The find icon is displayed
这是该片段的屏幕截图,您可以看到查找图标仍然存在.
This is the screenshot of the fragment as you can see the find icon is still there.
activity_main.xml
包含工具栏
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/tbMain"
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
这是我在MainActivity.java中创建菜单的代码
here is my code for create the menu in MainActivity.java
private void setupToolBar() {
mToolbar = (Toolbar)findViewById(R.id.tbMain);
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
在我的片段中,我有这个,正如您所看到的,我正在膨胀friends.xml菜单.
And in my fragment I have this, as you can see I am inflating the friends.xml menu.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.friends, menu);
}
非常感谢您的任何建议,
Many thanks for any suggestions,
推荐答案
我不确定您是否可以使用onCreateOptionsMenu()
执行此操作.我认为您最好的选择是 onPrepareOptionsMenu()
.
I'm not sure if you can do this with onCreateOptionsMenu()
. I think your better bet will be onPrepareOptionsMenu()
.
You can force Android to refresh the options menu by simply writing getActivity().invalidateOptionsMenu()
in Fragment's onResume()
.
所以您的onPrepareOptionsMenu()
如下所示:
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear(); //remove all items
getActivity().getMenuInflater().inflate(R.menu.menu_fragment, menu);
}
这篇关于在工具栏上显示活动和片段的不同菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!