协调器布局中带有锚点的 FAB 在 android pre-lollipop 中有额外的边距

FAB with anchor in coordinator layout has extra margin in android pre-lollipop

我有一个 CoordinatroLayoutFloatingActionButton。这是我的代码:

<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"
        android:layout_below="@+id/toolbar_layout"
        android:layout_above="@+id/actionbar">

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

            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:minHeight="?android:attr/actionBarSize"
                android:background="@color/toolbar_color" />


            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                >

            </ScrollView>


        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:clickable="true"
            app:fabSize="mini"
            android:src="@mipmap/ic_action_edit"
            app:layout_anchor="@id/toolbar"
            app:layout_anchorGravity="bottom|right|end"
            app:backgroundTint="@color/toolbar_color"            />
        </android.support.design.widget.CoordinatorLayout>

但它在棒棒糖和前棒棒糖设备中的表现有所不同。

棒棒糖:

棒棒糖之前:

其实我没有添加任何保证金。但是FAB在前棒棒糖设备上有利润。

我在 cheessesquare 示例中也看到了这个问题。它也显示不同的边距。有什么问题?

根据 ,这似乎是 android 设计库中的错误。它说:

in API <20, the button renders its own shadow, which adds to the overall logical width of the view, whereas in API >=20 it uses the new Elevation parameters which don't contribute to the view width.

所以我必须提供两个保证金资源文件:

res/values:

<dimen name= "fab_margin_right">0dp</dimen>

并且在 res/values-v21:

<dimen name = "fab_margin_right">8dp</dimen>

自22.2.1版本开始支持设计库,之前的回答不再正确。如果 FAB 在 CoordinatorLayout 中,则没有额外的填充。

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_show_qr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@mipmap/ic_action_edit"
        app:backgroundTint="@color/primary"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal"
        app:rippleColor="@color/primary_dark"/>
</android.support.design.widget.CoordinatorLayout>

此代码将在每个 Android 版本上生成以下 FAB。

我认为你不想让它们没有边距。如果我没理解错的话,你这样做是为了了解 android.

的不同版本发生了什么

You can use app:useCompatPadding="true" and remove custom margins to maintain same margins across different versions of android

概念验证