2

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

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 底部的问题。

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

4

2 回答 2

2

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

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

layout_anchorGravity您可以使用 FAB 中的属性设置 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>

看一下这个。 希望这可以帮助你。

于 2015-12-14T10:21:06.880 回答
1

使用相对布局最容易将 FAB 定位在两个视图之间。您可以使用 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>
于 2015-04-30T16:53:13.640 回答