com.android.support:design:24.2.1 中的浮动操作按钮错误

Floating Action Button bug in com.android.support:design:24.2.1

最近更新com.android.support:design库后,我有一个奇怪的错误:

预期的结果是第一个。然而,大约有 40% 的时间,FAB 忽略了锚点。 这也发生在 new 25.0.0 版本的设计支持库中。

这里是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_gravity="center"
android:background="@color/black_semi_transparent"
android:clickable="true"
android:orientation="vertical"
android:baselineAligned="false">

<Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:background="@android:color/transparent"
    android:onClick="onBackPressed" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="12"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:onClick="onBackPressed" />

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="18" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/linearlayout_anchor"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="@color/red_primary_color"
                android:orientation="vertical" >

                ...

            </LinearLayout>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="6"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" >

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

                    ...

                </LinearLayout>
            </ScrollView>
        </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_more_vert_white_36dp"
            app:layout_anchor="@id/linearlayout_anchor"
            app:layout_anchorGravity="bottom|right|end"/>

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

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:onClick="onBackPressed" />

</LinearLayout>

<Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:background="@android:color/transparent"
    android:onClick="onBackPressed" />

</LinearLayout>

有什么想法吗?

是的 bug。对于解决方法,您可以按照以下方式进行。

yourView.post(new Runnable() {
    @Override
    public void run() {
        yourView.requestLayout();
    }
});

其中 "yourView" 是定位视图。