首页 > 解决方案 > BottomNavigationView 上的工具栏位置错误

问题描述

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

底部导航:

底部导航

没有底部导航:

没有底部导航

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

<?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,但它的位置仍然错误。

奇数空间

使用 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+,但我认为这不是问题,我不知道如何协调这种情况。有没有人经历过?

感谢您的任何评论,特别是为什么它会发生,因为我无法理解它。

标签: androidandroid-linearlayoutandroid-coordinatorlayoutbottomnavigationviewandroid-bottomnavigationview

解决方案


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

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">

这是该属性的描述:

系统窗口是系统正在绘制非交互式(在状态栏的情况下)或交互式(在导航栏的情况下)内容的屏幕部分。

大多数情况下,您的应用不需要在状态栏或导航栏下进行绘制,但如果这样做:您需要确保交互元素(如按钮)不会隐藏在它们下方。这就是 android:fitsSystemWindows="true" 属性的默认行为:它设置 View 的填充以确保内容不会覆盖系统窗口。

我在这里找到了它:

为什么我要适合SystemWindows?


推荐阅读