BottomNavigationView 上错误位置的工具栏

Toolbar in the wrong location on BottomNavigationView

我正在构建一个应用程序,当我使用 BottomNavigationView 加载视图时,我总是遇到奇怪的问题,有时,我有一个额外的 space,其他时候,工具栏位置错误,例如:

带底部导航:

没有底部导航:

这是我对第一张图片的代码:

<?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"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">    
        <include
            layout="@menu/toolbar_recipe" />
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent" />
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:labelVisibilityMode="unlabeled"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color" />
</LinearLayout>
</RelativeLayout>

工具栏:

<?xml version="1.0" encoding="UTF-8" ?>
<android.support.v7.widget.Toolbar 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_recipe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:theme="@style/ToolBarStyle"
    android:minHeight="?android:attr/actionBarSize"
    android:background="@color/colorPrimary"/>

这就是 CoordinatorLayout 的行为方式,在我将 paddingTop 设置为 ?attr/actionBarSize 之后,FrameLayout 移动了一些 space,但它的位置仍然错误。

使用 CoordinatorLayout:

<?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"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_above="@+id/bottom_navigation">
        <include
            layout="@menu/toolbar_recipe" />
        <FrameLayout
            android:id="@+id/content_frame"
            android:paddingTop="?attr/actionBarSize"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:labelVisibilityMode="unlabeled"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color" />
</RelativeLayout>

如果没有那个添加,它只会留在后面。我在 Android 8+ 工作,但我不认为这是问题所在,我不知道如何协调这种情况。有人体验过吗?

感谢您的任何评论,尤其是为什么会这样,因为我无法理解。

我根据这一行找到了答案:

android:fitsSystemWindows="true"

我需要像这样将它添加到根元素中:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">

这里是 属性 的描述:

System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content.

Most of the time, your app won’t need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren’t hidden underneath them. That’s what the default behavior of the android:fitsSystemWindows="true" attribute gives you: it sets the padding of the View to ensure the contents don’t overlay the system windows.

我在这里找到它:

Why would I want to fitsSystemWindows?