首页 > 解决方案 > FloatingActionButton 转义锚视图的边界

问题描述

我有这样的布局:

<CoordinatorLayout>
    <NestedScrollView
        android:id="@+id/contentScrollView
        android:layout_width="600dp"
        android:layout_Height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ViewStub
            android:id="@+id/viewStub"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </NestedScrollView>

    <FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_anchor="@id/contentScrollView"
        app:layout_anchroGravity="bottom|right|end" />
</CoordinatorLayout>

现在,FAB 工作正常,但它逃脱了嵌套滚动视图的边界。它与底部边缘正确对齐,但它位于内容右边缘的中间(即 FAB 的一半在边界内,而另一半不在边界内)。

如果嵌套滚动视图的宽度设置为match_parent,则它可以正常工作 - 这就是它在手机上的布局方式。但是,我希望滚动内容在平板电脑上具有恒定宽度,并且 FAB 应该在内容内部,而不是在内容之外。

如何将 FAB 设置在嵌套滚动视图的范围内?边距和填充似乎对水平对齐没有任何作用。

标签: androidxmlandroid-layoutfloating-action-buttonandroid-coordinatorlayout

解决方案


尝试将android:layout_gravityFloatingActionButton 设置为相反的值,即top|left|start


推荐阅读