【发布时间】:2016-11-14 19:12:08
【问题描述】:
我将MaterialDrawer 与MainDrawerActivity 一起使用,其中我根据所选项目替换容器 FrameLayout 中的每个片段,但我想添加一个交互的FAB(仅用于此片段)使用CoordinatorLayout,所以它可以处理很酷的动画。
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,但它不起作用...
为什么?
【问题讨论】:
-
我也有同样的问题。你修好了吗?
-
是的,您需要在主布局中有 FAB,应该是协调器布局的直接子级才能工作
标签: android android-fragments material-design android-coordinatorlayout floating-action-button