FloatingActionButton 在 CoordinatorLayout 中的 NestedScrollView 上方添加了额外的边距

FloatingActionButton is adding extra margin above NestedScrollView in CoordinatorLayout

我正在为 CoordinatorLayout 中 FloatingActionButton 的边距奇怪行为而苦恼。 FAB 增加了额外的保证金,该保证金会在某些操作后消失。

我按照 Cheesesquare 示例创建了详细视图。我有完全相同的 xml 结构。唯一的区别是我将此视图用于片段而不是原始 activity.

这种情况大多数时候都会发生,但有时不会。对于片段对象只发生一次,一旦它自身修复它就可以正常工作。在我的第二个 phone 上,它没有发生。去掉FAB后就ok了。当我在日历中更改某事时,它也会导致边距自行修复。

附加启用布局线的 gif,以便您可以看到边距。

供参考:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content2"
    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"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    tools:context="com.habitsteacher.android.view.fragment.show.ShowHabitFragment">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:fitsSystemWindows="true"
        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"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="72dp"
            app:expandedTitleMarginEnd="24dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <TextView
                android:id="@+id/intentionText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="24dp"
                android:layout_marginLeft="48dp"
                android:layout_marginRight="24dp"
                android:layout_marginStart="48dp"
                android:ellipsize="end"
                android:fitsSystemWindows="true"
                android:maxLines="2"
                android:textColor="@color/icons"
                android:textSize="@dimen/abc_text_size_subhead_material"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.25"/>

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

            </android.support.v7.widget.Toolbar>

        </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:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/habit_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            android:orientation="vertical"
            android:paddingTop="32dp">

            <android.support.v7.widget.CardView
                android:id="@+id/calendarWrapper"
                android:layout_width="match_parent"
                android:layout_height="343dp"
                android:layout_margin="@dimen/card_margin">
            </android.support.v7.widget.CardView>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floating_button_edit_habit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"

        android:clickable="true"
        android:src="@drawable/ic_mode_edit_white_24dp"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"/>

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

有没有想过可能是什么原因,或者如何更深入地调查?

看起来 CoordinatorLayout 在作为非第一个添加到 activity 的片段中无法正常工作。这看起来像是SDK中的错误,但我无法准确定位根本原因。

因为只有当布局位于第一个添加到 activity 的片段时它才能正常工作,我将具有此布局的片段移动到单独的 activity 并且现在可以正常工作。

不过,它是在避免这种行为而不是修复它,所以任何答案仍然值得赞赏。