如何在布局边框顶部对齐 FAB

How to align a FAB on top of a Layout border

我正在尝试创建以下设计,其中有 2 个 FAB 和一个矩形布局。我可以将 FAB 对齐到不同的位置,但并不完全像图像中那样。使用 app:layout_anchorGravity="top|right|end" 我得到它恰好在角落,但图像中的那个略低于右上角,而左边的那个在左下角上方。

下面是我的xml文件,它只包含一个FAB。如果我设法完成第一个,那么第二个 FAB 很容易实现:

<android.support.design.widget.CoordinatorLayout 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">


<LinearLayout
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal"
    android:layout_margin="100dp"
    android:background="#403AA0F5">

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginTop="60dp"
    android:clickable="true"
    app:layout_anchor="@id/myLayout"
    app:layout_anchorGravity="top|right|end"/>

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

您可以使用约束布局创建此布局。相应地更改您的测量值。我给的矩形宽度为 200dp,高度为 100dp。这是代码:

<android.support.constraint.ConstraintLayout 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">


<View
    android:id="@+id/myLayout"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:background="#403AA0F5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginBottom="60dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:clickable="true"
    app:layout_constraintBottom_toBottomOf="@+id/myLayout"
    app:layout_constraintEnd_toEndOf="@+id/myLayout"
    app:layout_constraintHorizontal_bias="0.607"
    app:layout_constraintStart_toEndOf="@+id/myLayout" />

<android.support.design.widget.FloatingActionButton
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/myLayout"
    app:layout_constraintEnd_toStartOf="@+id/myLayout"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/myLayout" />

</android.support.constraint.ConstraintLayout>

这是设计的小片段:

试试这个:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:clickable="true"
        android:layout_gravity="right" />

还有这个:

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:clickable="true"
            android:layout_gravity="left|bottom" />