滚动时BottomNavigationView会遮挡Fragment的内容

3
我已经进行了大量的研究,但不知道该怎么办——我有一些内容在片段中被底部导航栏剪切掉了,不确定该怎么做。我尝试将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编写的,但我不认为问题出在任何实际代码中,而是在布局中。

@azizbekian 当然,我会在工作后有些时间的时候完成它! - secondubly
@AmjadKhan 我试过了,但并没有帮助。我仍然得到了与图片#2相同的结果。 - secondubly
你可以尝试使用LinearLayout代替RelativeLayout吗?并且你可以在home_fragment.xml中删除CoordinatorLayout,它是无用的。 - David Seroussi
@DavidSeroussi,我按照你的建议做了,但没有明显的变化 - 我会用新代码更新我的帖子! - secondubly
我认为问题来自于NestedScrollView的高度恒定,还有其他混乱的约束。你应该尝试直接在xml中构建你的布局,而不是在编辑模式下,并避免使用常量。尝试只使用屏幕大小无关的约束。如果不成功,我会发布一个完整的答案。 - David Seroussi
显示剩余5条评论
3个回答

3

我不确定预期行为是什么,只通过阅读你的问题无法理解。应用了这个补丁之后,仅包含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>

然后你会得到这个输出: enter image description here

2
运行得非常好!非常感谢! - secondubly

0

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

如果您想要将标题图像放在彼此上方,请将它们包装在FrameLayout中。为容器分配一个ID,并添加: app:layout_constraintTop_toBottomOf="@+id/headerImageContainer" 到NestedScrollView中。

然后,您可能可以摆脱RelativeLayout并用另一个ConstraintLayout替换它,仅使用约束来结构化其余的视图。

此外,根据Material Design的定义,BottomNavigationView的高度为56dp,您可以直接分配它,而不是在activity_home.xml中使其包裹内容。

<ConstraintLayout ...>

    <FrameLayout ...>
        <ImageView .../>

        <ImageView .../>

    </FrameLayout>

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

        </ConstraintLayout>

    </NestenScrollView>

</ConstraintLayout>

希望这能有所帮助。

只是为了确保我理解正确,当你说要摆脱RelativeLayout时,你指的是NestedScrollView内部的那个吗?如果是这样的话,那是不可能的,因为NestedScrollViews只允许一个直接子元素,所以我需要在其中放置某种形式的布局,以避免出现错误。 - secondubly
@TaiM。我稍微编辑了一下答案。我本来想在评论里加上的,但是没办法。这应该会有所帮助。 - theThapa
按照你的示例操作,但现在内容根本无法滚动,recyclerview可以工作,但是nestedscrollview本身无法正常运行。我想我要创建一个单独的存储库,只包含此代码部分,以查看是否有人可以重现我正在经历的问题,我觉得这可能比仅使用代码片段更容易。 - secondubly
我已将 Github 的链接添加到原帖中! - secondubly
1
@TaiM。我检查了你的项目并运行了它,scrollView的功能都符合预期,但响应速度有些慢。也许你可以尝试通过启用以下方式来优化你的recyclerView实现:featuredDestinationsRecycler.setItemViewCacheSize(10) featuredDestinationsRecycler.isDrawingCacheEnabled = true还有,在布局管理器中设置预取也是必要的:featuredDestinationsLayoutManager.initialPrefetchItemCount = 5 featuredDestinationsLayoutManager.isItemPrefetchEnabled = true如果有帮助,请点赞。 - theThapa

0
在你的ConstraintLayout中,你需要将layout_height设置为0dp,并将底部约束设置为BottomNavigationView的顶部。一个例子:
<FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接