使用 FAB 的片段布局与 CoordinatorLayout 冲突

Fragment layout with FAB conflict with CoordinatorLayout

我将 MaterialDrawerMainDrawerActivity 一起使用,其中我根据所选项目替换 容器 FrameLayout 中的每个片段,但我想添加一个与 CoordinatorLayout 交互的 FAB(仅针对此片段),以便它可以处理很酷的动画。

MainDrawer 布局:

<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?actionBarSize"
      android:minHeight="?actionBarSize"
      android:theme="@style/Toolbar"
      app:layout_scrollFlags="scroll|enterAlways" />

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

  <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

我如何替换每个片段:

try {
  supportFragmentManager.beginTransaction().replace(R.id.container, FeedFragment()).commit()
} catch (e: IllegalStateException) {
  Timber.i(e, "Fragment is still there.")
}

片段布局:

<FrameLayout 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.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:listitem="@layout/feed_item" />

  <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/spacing_medium"
    android:layout_marginRight="@dimen/spacing_medium"
    android:elevation="@dimen/elevation_little"
    app:fabSize="normal"
    app:layout_anchor="@+id/container"
    app:layout_anchorGravity="bottom|right"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:srcCompat="@drawable/ic_add_white_18dp" />

</FrameLayout>

但结果 FAB 按钮位于底栏后面:

我正在尝试设置协调器布局属性(layout_anchor 和那些)以匹配主布局 ID,但它不起作用... 为什么?

在 Activity 中定义浮动操作按钮 - 目前片段的容器被视为父级。

FAB 应该是 CoordinatorLayout

的直系子代