滚动时,片段内容隐藏在BottomNavigationView后面

我已经深入研究了这一点,并且在做什么的时候感到不知所措 – 我在活动中的BottomNavigation栏截取了一个片段中的一些内容,我不知道该怎么做。 我尝试添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到NestedScrollView但内容的底部(位置的名称)仍然被切断 – 这可能是一个简单的解决办法,但我不能想办法。 我的主要活动和“家”片段的XML如下所示:

activity_home.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/home_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/app_bar" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="start" android:background="@color/navbarBackground" app:menu="@menu/bottom_nav_menu" /> </LinearLayout> 

home_fragment.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:id="@+id/home_fragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp"> <ImageView android:id="@+id/tokyo_placeholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitStart" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/main_screen_placeholder" /> <ImageView android:id="@+id/airplane_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/circle_background" app:layout_constraintBottom_toBottomOf="@+id/tokyo_placeholder" app:layout_constraintLeft_toLeftOf="@+id/tokyo_placeholder" app:layout_constraintRight_toRightOf="@+id/tokyo_placeholder" app:layout_constraintTop_toTopOf="@+id/tokyo_placeholder" app:srcCompat="@drawable/icons8_airplane_48" tools:layout_constraintBottom_creator="1" tools:layout_constraintLeft_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintTop_creator="1" /> <android.support.v4.widget.NestedScrollView android:id="@+id/scroll_view" android:layout_width="0dp" android:layout_height="286dp" android:layout_marginBottom="@dimen/app_bar_height" android:layout_marginStart="5dp" android:layout_marginTop="5dp" android:fillViewport="true" app:layout_constraintBottom_toTopOf="@id/bottom_nav" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/tokyo_placeholder" tools:layout_constraintLeft_creator="1" tools:layout_constraintRight_creator="1"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/destination_headline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/featured_destinations_headline" android:textAllCaps="true" android:textAppearance="@style/TextAppearance.AppCompat.Headline" android:textSize="14sp" android:textStyle="bold" /> <android.support.v7.widget.RecyclerView android:id="@+id/featured_destinations_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/destination_headline" /> <TextView android:id="@+id/saved_trips_headline" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/featured_destinations_recycler_view" android:fontFamily="sans-serif" android:text="@string/saved_trips" android:textAllCaps="true" android:textAppearance="@style/TextAppearance.AppCompat.Headline" android:textSize="14sp" android:textStyle="bold" /> <android.support.v7.widget.RecyclerView android:id="@+id/saved_trips_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/saved_trips_headline" /> </RelativeLayout> </android.support.v4.widget.NestedScrollView> </android.support.constraint.ConstraintLayout> 

app_bar.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:elevation="4dp" android:background="#fff" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

举一个例子来说明我的意思, 这里是导航栏移开 的内容,这里是栏中的内容 。 我在想,这个问题是某种保证金/填充的问题,但不能完全弄清楚要解决的问题。

编辑:我已经创建了一个相关的代码库(和一个可行的模拟器的例子) 在这里 – 请注意,代码是用Kotlin编写的,但我不认为这个问题存在于任何实际代码中,而是更多在布局内

我不知道什么是预期的行为,我不明白,只是通过阅读你的问题。 已应用此修补程序 ,其中只包括home_fragment.xml布局更改:

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:id="@+id/home_fragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp"> <FrameLayout android:id="@+id/header_container" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/tokyo_placeholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitStart" app:srcCompat="@drawable/main_screen_placeholder" tools:ignore="ContentDescription" /> <ImageView android:id="@+id/airplane_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:background="@drawable/circle_background" app:srcCompat="@drawable/icons8_airplane_48" tools:ignore="ContentDescription" /> </FrameLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/scroll_view" android:layout_width="0dp" android:layout_height="0dp" android:fillViewport="true" android:paddingLeft="5dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/header_container"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/destination_headline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/featured_destinations_headline" android:textAllCaps="true" android:textAppearance="@style/TextAppearance.AppCompat.Headline" android:textSize="14sp" android:textStyle="bold" /> <android.support.v7.widget.RecyclerView android:id="@+id/featured_destinations_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/destination_headline" app:layout_constraintBottom_toTopOf="@id/saved_trips_headline" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/destination_headline" /> <TextView android:id="@+id/saved_trips_headline" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="sans-serif" android:text="@string/saved_trips" android:textAllCaps="true" android:textAppearance="@style/TextAppearance.AppCompat.Headline" android:textSize="14sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/featured_destinations_recycler_view" /> <android.support.v7.widget.RecyclerView android:id="@+id/saved_trips_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@id/app_bar" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/saved_trips_headline" /> </android.support.constraint.ConstraintLayout> </android.support.v4.widget.NestedScrollView> </android.support.constraint.ConstraintLayout> 

然后你会得到这个输出:

在这里输入图像描述

@ Tai M.问题似乎是NestedScrollView的硬编码高度。 由于您已经使用了ConstraintLayout,因此您可以轻松地放置此视图而无需添加其他相对布局。

如果您想要将标题图像置于另一个之上,请将其包装在FrameLayout中 。 为容器分配一个id,并将:app:layout_constraintTop_toBottomOf =“@ + id / headerImageContainer”添加到NestedScrollView。

然后,你可能会摆脱RelativeLayout,并用另一个ConstraintLayout来替换它,以仅使用约束来构造剩余的视图。

此外,通过材质设计定义,BottomNavigationView的高度为56dp ,您可以直接分配,而不是在activity_home.xml中包装内容

 <ConstraintLayout ...> <FrameLayout ...> <ImageView .../> <ImageView .../> </FrameLayout> <NestenScrollView ... wrap_content> <ConstraintLayout ...> <TextView .../> <RecyclerView .../> <!-- Assign constraints accordingly --> <TextView .../> <RecyclerView .../> </ConstraintLayout> </NestenScrollView> </ConstraintLayout> 

希望这可以帮助。