在Android 5.0以下版本中,使用CoordinatorLayout中的锚点FAB会有额外的边距问题。

8

我有一个带有FloatingActionButtonCoordinatorLayout。以下是我的代码:

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

但在不同版本的设备上,如棒棒糖和早期版本,它的显示方式不同。
棒棒糖:

enter image description here

Pre-Lollipop: 在此输入图片描述

实际上我没有添加任何边距。但是在 pre-lollipop 设备中,FAB 会有边距。

我也在 cheessesquare 示例中看到了这个问题。它也显示不同的边距。问题出在哪里?


当您添加边距时会发生什么?在Lollipop之前的版本中,它会使空间加倍吗? - Jude Fernandes
2
这篇文章可能对你有用:https://dev59.com/QV0a5IYBdhLWcg3wH1ly。 - DmitryArc
@JudeFernandes 是的,在Android 5.0以下版本中会使空间翻倍。 - Misagh Emamverdi
@DmitryArc,谢谢。链接很有帮助。所以这是Android中的一个bug! - Misagh Emamverdi
3个回答

19

我认为您不想在没有边距的情况下放置它们。如果我理解正确,您这样做是为了查看不同版本的Android中发生了什么。

您可以使用app:useCompatPadding="true"并删除自定义边距以保持在不同版本的Android上具有相同的边距

android studio code

概念证明

design view


我仍然不知道为什么没有其他答案描述相同的问题。我曾经遇到过同样的问题,现在已经解决了 :) - Saravanabalagi Ramachandran
谢谢!我已经更改了所选答案。 - Misagh Emamverdi

5
根据这个链接,似乎是安卓设计库中的一个bug。它说道:

在API <20中,按钮会渲染自己的阴影,这会增加视图的总逻辑宽度,而在API >=20中,它使用不会对视图宽度产生贡献的新Elevation参数。

因此,我需要为margin提供两个资源文件:
res/values:
<dimen name= "fab_margin_right">0dp</dimen>

在res/values-v21中:

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

3

从支持和设计库的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。

enter image description here


3
谢谢,但我在遇到问题时正在使用支持库23.0.0。 - Misagh Emamverdi
但如果它在另一个布局中,比如线性等,就会有问题:/ 安卓对开发者来说是愚蠢的,并且是一种侮辱。 - Amir Ziarati

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