仅在API 19上为FloatingActionButton添加额外的间距

3
我在API19上遇到了一个问题,即周围有额外的空白或间距。

API19上的截图如下:

enter image description here

其他每个版本上的Margin都是正确的,请参见以下截图:

enter image description here

两种情况下均打开了显示布局边界的开发者选项。您可以清楚地看到,在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
@ADM 很遗憾没有帮到我。请看我的编辑。 - Adam Varhegyi
4个回答

4
你可以通过编程的方式从floatingActionButton中删除margin。这是一个已知的问题,因为有额外的边距。
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);
}

编辑

尝试在FloatingActionButtonxml中使用这些属性。

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

遗憾的是没有帮助。空间仍然存在。 - Adam Varhegyi
@AdamVarhegyi 你试过我的更新答案了吗?它对你有用吗? - Jay Rathod

2
这是因为在Lollipop之前的设备上,FAB中有特殊的padding实现。
您可以使用

app:useCompatPadding="true"

要覆盖此行为,请使用以下方法。

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


0

如果您希望在所有 Android 版本上具有相同的填充效果,可以为 fab 设置 app:useCompatPadding="true"


我知道这并不能解决问题。这是安卓系统的问题,目前还没有被解决。这只是一种方法,使得在新版本中FAB具有相同的边距,以避免因不同版本而出现不同的外观。 - user8959091
啊哈,所以它在新版本上添加了额外的间距而不是在旧版本上删除它,对吗? - Adam Varhegyi

0

对于那些使用材料浮动操作按钮的人

app:fabCustomSize="@dimen/your_size"

为了消除FAB周围的额外填充,您可以覆盖来自Material库的尺寸。只需将此行放入您的dimen.xml文件中即可:

<dimen name="mtrl_fab_min_touch_target" tools:override="true">@dimen/your_size</dimen>

之前 输入图像描述

之后 输入图像描述


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