在抽屉+底部导航中动态更改工具栏菜单项

时间:2018-10-03 13:58:01

标签: java android android-fragments android-toolbar android-bottomnav

我创建了一个DrawerNavigation,它的MenuItem(主页)具有一个BottomNavigationBar。对于BottomNavigationBar中的MenuItem“Hinzufügen”,我在相应的片段(AddFragment.java)

中为工具栏添加了自定义MenuItem。

AddFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
    }
    setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.save_toolbar_menu, menu);
}

所有其他片段的工具栏项将在MainActivity中膨胀:

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

Drawer Navigation

"Hinzufügen" view

问题

当我在BottomNavigationBar项目之间切换时,工具栏菜单将按预期工作。就是说,“Hinzufüen”菜单显示R.menu.save_toolbar_menu,当我选择另一个MenuItem时,R.menu.toolbar_menu将膨胀到工具栏中。

Selected another BottomNavigationitem

但是:当我在BottomNavigationBar中选择“Hinzufügen”,然后从DrawerNavigation中选择一个MenuItem时(让我们说“ Einstellungen”时),工具栏菜单将不会恢复,或者R.menu.toolbar_menu不会再次膨胀。仅当我在BottomnavigationBar菜单之间进行切换时,此功能才正确。

Wrong Toolbar MenuItems

save_toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/toolbar_nav_cancel"
    android:title="@string/title_cancel"
    app:showAsAction="always" />
<item
    android:id="@+id/toolbar_nav_save"
    android:title="@string/title_save"
    app:showAsAction="ifRoom" />
</menu>

toolbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/toolbar_nav_fav"
    android:icon="@drawable/ic_favorite_white"
    android:title="@string/title_fav"
    app:showAsAction="ifRoom" />
</menu>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/main_content" />
</android.support.design.widget.CoordinatorLayout>

main_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

<FrameLayout
    android:id="@+id/main_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true" />

</RelativeLayout>

fragment_home.xml

<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<FrameLayout
    android:id="@+id/home_frame"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/home_navigation"
    app:layout_constraintBottom_toTopOf="@+id/home_navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/home_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/colorPrimary"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/bottom_nav_colors"
    app:itemTextColor="@color/bottom_nav_colors"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_navigation_menu" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案