工具栏在BottomNavigationView上的错误位置

时间:2019-07-06 17:34:29

标签: android android-linearlayout android-coordinatorlayout bottomnavigationview android-bottomnavigationview

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

使用底部导航:

bottomnavigation

无底部导航:

no bottom navigation

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

<?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移动了一些空间,但是它的位置仍然错误。

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

感谢您的任何评论,特别是为什么它会发生,因为我听不懂。

1 个答案:

答案 0 :(得分:0)

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

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”属性的默认行为   给您:它设置视图的填充以确保内容   不要覆盖系统窗口。

我在这里找到它

Why would I want to fitsSystemWindows?