【问题标题】:Toolbar in the wrong location on BottomNavigationViewBottomNavigationView 上的工具栏位置错误
【发布时间】:2019-07-06 17:34:29
【问题描述】:

我正在构建一个应用程序,当我使用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+,但我认为这不是问题,我不知道如何协调这种情况。有人经历过吗?

感谢您的任何评论,尤其是我无法理解的原因。

【问题讨论】:

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


    【解决方案1】:

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

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多