FloatingActionButton在Lollipop中的默认间距设置无效

24

我在 CoordinatorLayout 中显示了一个带有RecyclerViewFloatingActionButton,当点击 FAB 时,会在RecyclerView中添加一行。现在的问题是,在 API 15 中测试时,FAB 有边距,但在 API 22 (Lollipop 5.1.1-Nexus 4) 中测试时,没有任何边距,FAB 被按到屏幕边缘。

我的问题是,在 API 15 中 FAB 具有默认的边距设置(未测试其他设备),但在 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>

在此输入图像描述


1
https://dev59.com/QV0a5IYBdhLWcg3wH1ly - williamj949
根据我看到的链接,K基于库存在问题。 - Psypher
3个回答

21

只需使用:

app:useCompatPadding="true"

这应该会增加所需的间隔空间。


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

@dimen/action_button_margin 在 values/dimens.xml 中被定义:

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

同时在values-v21/dimens.xml文件中:

<dimen name="action_button_margin">16dp</dimen>

0

我猜你遇到的问题和这个问题描述的一样。不同的边距是由于阴影是否根据API版本进行计算。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接