CollapsingToolbarLayout sometimes leaves white spa

2019-06-16 04:10发布

This question already has an answer here:

I have a problem. Here are the screenshots:

enter image description here

enter image description here

enter image description here

And here is the code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleMarginStart="64dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yo Yo" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/codelab_fab_margin_bottom"
        android:layout_marginRight="@dimen/codelab_fab_margin_right"
        app:borderWidth="0dp"
        app:fabSize="normal" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/colorPrimaryDark"
    app:itemTextColor="@color/colorPrimary"
    app:menu="@menu/navigation_drawer_items" />

You can see sometimes, a white space appears below the Toolbar, but sometimes no. Dou yo have any idea of what's wrong? Because I really don't know how to fix this.

Thanks in advance,

Edit: I can only reproduce this with Android 5.1.1 in the Nexus 7

2条回答
beautiful°
2楼-- · 2019-06-16 04:52

Add android:fitsSystemWindows="true" to CoordinatorLayout, AppBarLayout and CollapsingToolbarLayout.

查看更多
Evening l夕情丶
3楼-- · 2019-06-16 04:53

Try to insert this attributes in NestedScrollView to force the correct behavior, this is a workaround and works for me:

    android:paddingTop="0dp"
    android:layout_marginTop="0dp"
查看更多
登录 后发表回答