浮动操作按钮行为随机

Floating action button behaves randomly

我正在尝试使用浮动作为选择卡片视图的指示器。

我希望 FAB 位于卡片的右上角,有一点重叠。我尝试使用 drawable 实现相同的外观,但由于该应用程序针对 API 16+,因此我无法使用海拔将其绘制在卡片上方。

使用这个 xml:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <android.support.v7.widget.CardView
        android:id="@+id/suppTemplateCardViewList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

        <ImageButton
            android:id="@+id/suppFunction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null" />

        <TextView
            android:id="@+id/suppFunctionName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/selectorIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="mini"
        app:layout_anchor="@id/suppTemplateCardViewList"
        app:backgroundTint="@color/blueish"
        android:src="@drawable/ic_done"
        app:layout_anchorGravity="top|end"/>

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

我有这种行为:

我也试过使用

app:layout_anchorGravity

标签,但无济于事。 谢谢。

"top|end"是问题的核心。改成"top|right|end"

其实最后我自己发现了。显然,xml 中的顺序很重要,因此将 FAB 设置在卡片视图上方使其有效。