Android BottomNavigationView在使用具有滚动视图行为的工具栏时覆盖内容

Android BottomNavigationView在使用具有滚动视图行为的工具栏时覆盖内容,android,android-coordinatorlayout,bottomnavigationview,android-appbarlayout,android-nestedscrollview,Android,Android Coordinatorlayout,Bottomnavigationview,Android Appbarlayout,Android Nestedscrollview,我很难让AppBarLayout、NestedScrollView和BottomNavigationView正常工作。我的问题是,当我在NestedScrollView上设置app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”时,它会延伸到BottomNavigationView后面,如图所示。 所以问题是底部导航栏覆盖了内容,而不是停留在导航顶部的内容 我已经尝试了很多解决方案,包括将布局包装在Relati

我很难让AppBarLayout、NestedScrollView和BottomNavigationView正常工作。我的问题是,当我在NestedScrollView上设置
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
时,它会延伸到BottomNavigationView后面,如图所示。

所以问题是底部导航栏覆盖了内容,而不是停留在导航顶部的内容

我已经尝试了很多解决方案,包括将布局包装在RelativeLayout中,并将BottomNavView放在其中,而不是放在CoordinatorLayout中

这是我所附的示例项目的基本布局

<androidx.coordinatorlayout.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="com.example.android.navigationadvancedsample.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/app_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true">
        <FrameLayout
            android:id="@+id/nav_host_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/bottom_nav"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

一个复制该问题的小样本项目(基于谷歌的导航组件样本)。有人能告诉我我做错了什么吗?

在你的代码中,你的NestedScrollView占据了整个屏幕。使用带有权重的垂直线性布局,您可以将其设置为NestedScrollView在导航栏顶部停止的位置,如您所愿

<androidx.core.widget.NestedScrollView
    android:id="@+id/app_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="0dp" *** changed from match_parent to 0dp
    android:layout_weight="1" *** added weight to fill remaining screen
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true">
    <FrameLayout
        android:id="@+id/nav_host_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/> *** changed from 0dp to match_parent
</androidx.core.widget.NestedScrollView>

***已从0dp更改为匹配\u父级

现在的设置方式考虑了导航栏,并扩展了NestedScrollView的布局,以填充屏幕上剩余的空白。现在NestedScrollView将不会扩展到导航栏之外。

不确定。但看起来像是在预览中工作。将nestedScrollView和BottomNavigation视图放入相对布局中

 <RelativeLayout android:layout_width="match_parent" 
android:layout_height="match_parent">


<androidx.core.widget.NestedScrollView
        android:id="@+id/app_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_nav"
        android:fillViewport="true" android:layout_marginBottom="-2dp">
    <FrameLayout
            android:id="@+id/nav_host_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
</androidx.core.widget.NestedScrollView>

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/bottom_nav_menu"/>
</RelativeLayout>

您只需将BottomNavigationView从CoordinatorLayout中取出,并将两者都放在RelativeLayout中即可


我也面临同样的问题,并找到了解决办法。希望有帮助。

您可以对底部导航应用布局行为,以在滚动中隐藏它。检查此处:@Billalnaeem不幸的是,客户端的要求是底部导航必须始终可见。那么,最简单的解决方案是为嵌套的ScrollView提供与底部导航高度相等的底部填充。如果您将所有内容都包装在线性布局中,并将底部导航放在底部,则它将起作用。我目前正在开发一个具有这种布局的应用程序。@BilalNaeem这看起来像是一个黑客,我相信一定有更好的解决方案。此外,当我这样做时,我可以滚动内容通过导航栏。它应该停在导航栏的顶部。谢谢你的建议,但这会破坏滚动行为。如果我尝试将布局行为放在相对布局上,那么底部导航会滚动内容,而不是停留在底部。您可以发布整个屏幕的xml吗?我很难理解线性布局的走向。您的意思是包装CoordinatorLayout(在这种情况下,关于布局权重的更改将不起作用)还是包装NestedScrollView(在这种情况下,滚动布局_行为将不再起作用)?