【问题标题】:FloatingActionButton is adding extra margin above NestedScrollView in CoordinatorLayoutFloatingActionButton 在 CoordinatorLayout 中的 NestedScrollView 上方添加了额外的边距
【发布时间】:2015-11-24 23:32:25
【问题描述】:

我在 CoordinatorLayout 中使用 FloatingActionButton 处理奇怪的边距行为。 FAB 添加额外的边距,在某些操作后消失。

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

这在大多数情况下都会发生,但有时不会。片段对象只发生一次,一旦它自行修复它就会很好地工作。在我的第二部手机上,它不会发生。删除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>

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

【问题讨论】:

    标签: android android-layout android-design-library android-coordinatorlayout floating-action-button


    【解决方案1】:

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

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

    不过,它避免了这种行为而不是修复它,所以仍然感谢任何答案。

    【讨论】:

    • 对此问题有任何见解吗?当我以编程方式交换添加视图以添加交叉淡入淡出时,我得到了相同的行为。
    • 并非如此。为您的案例应用我的解决方法可能会将这些视图移动到单独的活动并在它们之间切换。我希望我知道合适的解决方案,但我真的找不到任何东西,调试 android 布局管理器是一项艰巨的任务。