【问题标题】:FAB with anchor in coordinator layout has extra margin in android pre-lollipop在协调器布局中带有锚点的 FAB 在 android pre-lollipop 中有额外的边距
【发布时间】:2015-12-21 00:59:28
【问题描述】:

我有一个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 android-layout floating-action-button android-coordinatorlayout


【解决方案1】:

我不认为你想把它们放在没有边距的地方。如果我理解正确,您这样做是为了查看不同版本的 android 中发生了什么。

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

概念证明

【讨论】:

  • 我仍然不知道为什么没有其他答案描述相同。我遇到了同样的问题,现在解决了:)
  • 谢谢!我已更改所选答案。
【解决方案2】:

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

在 API =20 中它使用新的 不影响视图宽度的高程参数。

所以我要为margin提供两个资源文件:

分辨率/值:

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

    【讨论】:

    • 谢谢,但当我遇到问题时,我使用的是支持库 23.0.0。
    • 但是如果它在另一个布局中,比如线性等:/ android 是愚蠢的,是对开发人员的侮辱
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多