FloatingActionButton默认边距在棒棒糖中不起作用

Psy*_*her 23 android floating-action-button androiddesignsupport coordinator-layout android-coordinatorlayout

我在FloatingActionButtona Recyclerview中显示a CoordinatorLayout,当单击FAB时,将添加一行Recyclerview.现在问题是当我在API 15中测试时FAB有余量,但是当我在API 22(棒棒糖5.1.1-Nexus 4)中测试时,我没有得到任何余量并且FAB被按到屏幕的边缘.

我担心的是FAB在API 15中有默认的边距设置(没有测试其他设备)但在API 22中没有,或者我错过了什么.

我的布局代码:

<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:background="#6fbababa"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/fab_padding_bottom"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:orientation="horizontal"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
            ---------------------
            ---------------------    
        </LinearLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_floating_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_plus"
        app:borderWidth="0dp"
        app:layout_anchor="@id/my_recycler_view"
        app:layout_anchorGravity="bottom|right|end"
        app:rippleColor="@color/wallet_highlighted_text_holo_light" />

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Daw*_*yży 18

只需使用:

app:useCompatPadding="true"
Run Code Online (Sandbox Code Playgroud)

这应该添加所需的空间.


Dan*_*cci 14

我最终使用了API特定的边距值.我的操作按钮是这样的:

    <android.support.design.widget.FloatingActionButton
    android:contentDescription="@string/menu_compose"
    android:id="@+id/action"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/ic_create_white"
    android:layout_gravity="bottom|right|end"
    android:layout_marginBottom="@dimen/action_button_margin"
    android:layout_marginRight="@dimen/action_button_margin"
    android:layout_marginEnd="@dimen/action_button_margin"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    app:fabSize="normal"/>
Run Code Online (Sandbox Code Playgroud)

@dimen/action_button_margin 在两个值/ dimens.xml中定义:

<dimen name="action_button_margin">0dp</dimen>
Run Code Online (Sandbox Code Playgroud)

在values-v21/dimens.xml中:

<dimen name="action_button_margin">16dp</dimen>
Run Code Online (Sandbox Code Playgroud)