BottomNavigationView наложение контента при использовании панели инструментов с поведением прокрутки - PullRequest
1 голос
/ 09 июля 2019

У меня возникают трудности при правильной работе AppBarLayout, NestedScrollView и BottomNavigationView.Моя проблема в том, что когда я устанавливаю app:layout_behavior="@string/appbar_scrolling_view_behavior" в NestedScrollView, он расширяется за BottomNavigationView, как показано здесь.View in layout editor

View in app

Таким образом, проблема заключается в том, что BottomNavBar накладывается на контент, а не останавливается в верхней частиNav.

Я испробовал много решений, включая оборачивание макета в RelativeLayout и помещение в него BottomNavView вместо CoordinatorLayout.

Вот базовый макет из примера проекта I 'Прилагается.

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

Вот небольшой пример проекта, который воспроизводит проблему (на основе примера компонентов навигации от Google).Может кто-нибудь сказать, пожалуйста, что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 09 июля 2019

Не уверен.Но, похоже, работает в режиме предварительного просмотра.Помещение nestedScrollView и BottomNavigation view в относительный макет.

 <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>
0 голосов
/ 09 июля 2019

В вашем коде ваш NestedScrollView занимал весь экран. Используя вертикальный LinearLayout с весами, вы можете добраться туда, где NestedScrollView останавливается в верхней части NavBar, как вы хотите.

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

Способ его настройки теперь учитывает NavBar и расширяет компоновку NestedScrollView, чтобы заполнить оставшееся пустое пространство на экране. Теперь NestedScrollView не будет расширяться за пределы NavBar.

...