Floating Action Button layout_margin(Bottom) 对下边距没有影响

Floating Action Button layout_margin(Bottom) has no effect on bottom margin

我正在 Android 6.0.1 设备上进行测试,问题是无论我使用哪个值,我的 FAB 都只对 layout_marginRight 而不是 layout_marginBottom 做出反应。

这是我的布局:

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

<ScrollView
    android:id="@+id/scrollViewUnitNames"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_margin="16dp"
    android:fillViewport="true"><!----></ScrollView>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    android:id="@+id/fab"
    android:src="@android:drawable/ic_dialog_email"
    app:fabSize="normal"
    app:borderWidth="0dp"
    />      </RelativeLayout>

因此,如果我将 FAB 对齐到左上角或右上角,则边距有效。 如果我将它对齐到左下角或右下角,layout_marginBottom 似乎没有效果。 我正在使用设计支持库 23.2.0。 我在 Android 4.0.3 模拟器上对其进行了测试并且它有效。 你知道如何解决这个问题吗?谢谢!

编辑 这是它的样子:

编辑 2 我现在知道的更多一点:在创建时,我将内容视图设置为空布局(内部只有一个相对布局)。经过一些测试后,我将布局更改为:

RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));

在一个新项目中,我做了同样的事情,在我改变布局之前它起作用了,在我改变它之后,它看起来和上图一样。这是更改布局的不正确方法吗?或者有更好的方法吗?感谢您的帮助。

FloatingActionButton 的 xml 更改为如下内容:

<android.support.design.widget.FloatingActionButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true"
      android:layout_marginLeft="@dimen/fab_margin"
      android:layout_marginTop="@dimen/fab_margin"
      android:layout_marginRight="@dimen/fab_margin"
      android:layout_marginBottom="50dp"
      android:clickable="true"
      android:id="@+id/fab"
      android:src="@android:drawable/ic_dialog_email"
      app:fabSize="normal"
      app:borderWidth="0dp"
      />

如果您使用 android:layout_marginandroid:layout_marginBottom,它将覆盖您的底部边距。单独定义每个边距将允许您拥有与 top/left/right.

不同的底部边距

更新的答案:

更改此行:

container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));

container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, container, false));

它应该更正 FloatingActionButton 上的边距。

在布局底部添加此视图

    <View
        android:id="@+id/bottomView"
        android:layout_width="@dimen/default_16dp"
        android:layout_height="bottom margin of the floating action button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"/>

并在此视图上设置浮动操作按钮

    android:layout_above="@id/bottomView"