【问题标题】:Extra margin(spacing) around FloatingActionButton only on API 19仅在 API 19 上 FloatingActionButton 周围的额外边距(间距)
【发布时间】:2018-07-09 10:54:29
【问题描述】:

我在 FloatingActionButton 周围遇到了一个额外的边距或间距,但仅限于 API19。

API19 截图:

Margin 在所有其他版本上都是正确的,请看下面的截图:

用于显示布局边界的开发者选项在这两种情况下都是打开的。您可以清楚地看到,在 API 19 中,FAB 周围有一个额外的空间。

XML:

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

                        <android.support.design.widget.FloatingActionButton
                            android:id="@+id/path_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="12dp"
                            android:layout_marginRight="12dp"
                            android:layout_marginTop="12dp"
                            android:background="@null"
                            app:backgroundTint="@color/blue_light"
                            app:srcCompat="@drawable/ic_line" />

                        <android.support.design.widget.FloatingActionButton
                            android:id="@+id/stream_toggle_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/path_btn"
                            android:layout_marginBottom="12dp"
                            android:layout_marginLeft="12dp"
                            android:layout_marginTop="12dp"
                            android:background="@null"
                            app:srcCompat="@drawable/ic_stream_video_white" />
                    </RelativeLayout>

请注意,XML 中的边距仅在屏幕截图上添加紫色区域。如果我删除边距,多余的间距不会消失。

如果可以的话,请帮忙。

谢谢。

编辑:

添加

  app:useCompatPadding="true"

FABS 无济于事。间距还在。

【问题讨论】:

  • 尝试设置app:useCompatPadding="false"
  • @ADM 可悲的是没有帮助。查看我的编辑。

标签: android android-layout floating-action-button


【解决方案1】:

您可以 programmaticallymarginfloatingActionButton 中删除。这是一个已知问题,这是因为额外的边距。

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) stream_toggle_btn.getLayoutParams();
    params.setMargins(0, 0, 0, 0); 
    stream_toggle_btn.setLayoutParams(params);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) path_btn.getLayoutParams();
    params.setMargins(0, 0, 0, 0); 
    path_btn.setLayoutParams(params);
}

编辑

尝试在 FloatingActionButton xml 中使用此属性。

app:elevation="0dp"
app:pressedTranslationZ="0dp"

喜欢

<android.support.design.widget.FloatingActionButton
    android:id="@+id/path_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="12dp"
    android:background="@null"
    app:backgroundTint="@color/blue_light"
    app:srcCompat="@drawable/ic_line"
    app:elevation="0dp"
    app:pressedTranslationZ="0dp"/>

【讨论】:

  • 可悲的是没有帮助。空间仍然存在。
  • @AdamVarhegyi 你试过我更新的答案对你有用吗??
【解决方案2】:

这是因为在棒棒糖之前的设备上,FAB 中的特殊填充实现。

你可以使用

app:useCompatPadding="true"

覆盖此行为。

boolean: 如果 FloatingActionButton 正在添加内部填充,则为 true Lollipop 及之后的平台,以确保所有平台的尺寸一致 平台。

【讨论】:

    【解决方案3】:

    如果您希望所有 android 版本都使用相同的填充,请为 fab 设置 app:useCompatPadding="true"

    【讨论】:

    • 我知道这并不能解决问题。这是android的问题,还没有解决。这只是使晶圆厂在新版本中具有相同边距的方法,这样您就不会因为版本不同而遇到不同的外观。
    • aaaahh,所以它确实在新版本上增加了额外的间距,而不是在旧版本上删除它,对吧?
    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 2011-12-10
    相关资源
    最近更新 更多