NestedScrollView的底部被导航栏遮挡了 [Android]

5
我在使用带有CollapsingToolbarLayout的布局中遇到了NestedScrollView的问题。当我向下滚动文本时,最后几句话被导航栏遮盖。

enter image description here
layout.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@style/ExpandedAppBar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Title">

            <ImageView
                android:id="@+id/toolbar_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/visit_at_office"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum" />
    </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.CoordinatorLayout>

在TextView中添加margin bottom可以修复这个bug,但我想知道是否还有更好的解决方案以及为什么会出现这种情况。请问有人能帮忙吗?

2个回答

5
如果仍然在寻找解决此问题的方法,那么问题的原因是CoordinatorLayout未正确计算CollapsingToolbarLayout的大小,因为它具有使用了app:layout_collapseMode="pin"设置的工具栏。 它认为CollapsingToolbarLayout将折叠到零高度,因此将所有可用空间留给NestedScrollView,但实际上工具栏仍然被固定,因此NestedScrollView会向下移动,在NavigationBar的背后。
最简单的解决方法就是在CollapsingToolbarLayout中添加android:minHeight="?attr/actionBarSize"(或您正在使用的任何工具栏高度),这样CoordinatorLayout将正确知道需要为NestedScrollView留多少空间。

@dudeck 我认为这是正确的答案,如果我们考虑类型崩塌,解释就会有很多意义。 - cutiko
刚刚遇到了这个问题,想说谢谢你的答案!省了我很多时间! - Benoit

0
你在根布局中添加了android:fitsSystemWindows="true",在你的情况下是CoordinatorLayout
这行代码将使视图适应整个屏幕。

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