Android Material 设计工具栏和 FAB 按钮交叉

Android Material Design Toolbar and FAB Button crossing over

在 Google 的 Material 设计规范中,他们经常显示浮动操作按钮位于工具栏的一半之上并且覆盖了内容。

http://www.google.com/design/spec/components/buttons-floating-action-button.html

但我已经尝试了一些变体,工具栏和内容之间仍然存在差距,这是由按钮引起的。

<LinearLayout>
 <include layout="@layout/toolbar" />   

  <include layout="@layout/fab_button" />

  <ScrollView>
    Content
  </ScrollView>
</LinearLayout>

我也尝试过将工具栏和 FAB 按钮都放在 FrameLayout 中,但它也有间隙。 FAB 按钮代码取自 Google 的样本,我没有遇到过让它重叠在 RecyclerView 底部的问题。

有没有办法实现 Material 设计规范中显示的这种外观。

使用相对布局最容易将 FAB 放置在两个视图之间。您可以使用晶圆厂的高程参数使其通过工具栏。将 FAB 的高度设置为高于工具栏的高度。

<RelativeLayout 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"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <ListView
        android:id="@+id/listview"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


    <com.getbase.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/favorite"
        android:layout_alignBottom="@+id/toolbar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:elevation="8dp"
        android:layout_marginBottom="-32dp"
        fab:fab_icon="@drawable/ic_favorite_outline_white_24dp"
        fab:fab_colorNormal="@color/accent"
        fab:fab_size="mini"
        />
</RelativeLayout>

在 FAB 中添加 layout_anchor 属性 并将其设置为顶视图。

CoordinatorLayout 作为您的根视图,这将是最佳布局实践。

您可以在 FAB 中使用 layout_anchorGravity 属性设置 FAB 重力。

<?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">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:background="@android:color/holo_blue_bright"
            android:orientation="horizontal"/>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@android:color/holo_green_light"
            android:orientation="horizontal"/>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_done"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|right|end"/>
    </android.support.design.widget.CoordinatorLayout>

Check this out. 希望对您有所帮助。