浮动操作按钮 layout_margin(Bottom) 对底部边距没有影响

nic*_*307 3 android position margin android-relativelayout floating-action-button

我正在 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>
Run Code Online (Sandbox Code Playgroud)

因此,如果我将 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));
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*end 5

将您的 xml 更改为FloatingActionButton如下所示:

<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"
      />
Run Code Online (Sandbox Code Playgroud)

如果您使用android:layout_marginand android:layout_marginBottom,它将覆盖您的底部边距。单独定义每个边距将允许您拥有与顶部/左侧/右侧不同的底部边距。

更新的答案:

改变这一行:

container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));
Run Code Online (Sandbox Code Playgroud)

container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, container, false));
Run Code Online (Sandbox Code Playgroud)

它应该更正FloatingActionButton.