FAB的片段布局与CoordinatorLayout冲突 [英] Fragment layout with FAB conflict with CoordinatorLayout

本文介绍了FAB的片段布局与CoordinatorLayout冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MaterialDrawer MainDrawerActivity替换了中的每个片段容器 FrameLayout基于选定的项目,但是我想添加一个与CoordinatorLayout交互的FAB(仅用于此片段),以便它可以处理很酷的动画.

Im using MaterialDrawer with a MainDrawerActivity where I replace each fragment inside container FrameLayout based on selected item, but I want to add a FAB (just for this fragment) that interacts with CoordinatorLayout so it can handle cool animations.

MainDrawer布局:

MainDrawer layout:

<?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按钮位于底部栏的后面:

But as result the FAB button is behind bottom bar:

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

Im trying setting the coordinator layout attribues (layout_anchor and those) to match the main layout ids but it is not working... why?

推荐答案

在Activity中定义浮动操作按钮-当前,片段的容器被视为父容器.

Define the Floating Action Button within the Activity - currently the Fragment's container is considered the parent.

FAB应该是CoordinatorLayout

这篇关于FAB的片段布局与CoordinatorLayout冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆