浮动操作按钮不在右下角

Floating Action Button Not In Bottom Right

出于某种原因,我的 FAB 不会像往常一样转到屏幕的右下角。我在一个片段中,那个片段是一个协调器布局(我不知道这是否是一个好的做法)。

这里是布局xml代码:

<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.gigstudios.polls.fragment.MyPollsFragment">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_36dp"
        app:fabSize="normal"
        app:layout_anchor="@id/linear_layout"
        app:layout_anchorGravity="bottom|right|end" />

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

出于某种原因,FAB 最终出现在右上角而不是右下角。有谁知道我做错了什么?

顺便说一下,xml 预览总是在右下角显示 fab 按钮,但是当我 运行 它在我的 phone 上时,它在右上角相反。

编辑: 这是我的 main_activity xml 以显示我放置片段的位置("container" FrameLayout)。

<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:background="@color/colorGrey">

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorGrey"
        android:layout_marginTop="?attr/actionBarSize">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/container">
        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

删除行

app:layout_anchor="@id/linear_layout"
app:layout_anchorGravity="bottom|right|end"

在你的 FloatingActionButton 上。他们没有做任何事情(因为您锚定到一个填满整个 CoordinatorLayout 的视图)。这将使您的 layout_gravity 得到尊重,您的 FloatingActionButton 将被放置在 CoordinatorLayout.

的右下角

由于 android.support.v4.widget.NestedScrollView 再添加一个字段 android:fillViewport="true". Issue was coming as yourNestedScrollView` 未扩展以匹配父级或可用 space,因此出现了此问题。用我的代码测试工作正常

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:fillViewport="true"
    android:layout_marginTop="?attr/actionBarSize">
</android.support.v4.widget.NestedScrollView>

从您的浮动操作按钮 xml 代码中删除 android:layout_gravity="bottom|end"...它将起作用