浮动操作按钮布局边距(底部)对底部边距没有影响。

3

我正在一台安装了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对齐到左上角或右上角,那么margin有效。但是如果我将其对齐到左下角或右下角,layout_marginBottom似乎没有效果。我正在使用的是设计支持库23.2.0版本,在Android 4.0.3模拟器上测试过了。你有任何想法如何解决这个问题吗?谢谢!
编辑:这是它的外观: enter image description here 编辑2:我现在知道更多情况了:在创建时,我将内容视图设置为空布局(只包含一个相对布局)。在进行一些测试后,我像这样更改了布局:
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.removeAllViews();
container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));

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

我已经在运行sdk 23的Nexus 6模拟器上尝试了这个布局,看起来一切正常。 - Chris Wilson
是的,请阅读被接受的答案。 - nic2307
2个回答

5

将您的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,它将覆盖底部边距。单独定义每个边距将允许您拥有与顶部/左侧/右侧不同的底部边距。 更新的答案: 更改此行:
container.addView(getLayoutInflater().inflate(R.layout.my_layout_file, null));

To

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

它应该纠正FloatingActionButton上的边距。

我不想让底部的边距与右侧的边距不同。我只是在测试它是否适用于不同的位置,我发现只有底部边距似乎不起作用。就像layout_marginBottom始终为零一样。 - nic2307
你能发一下你所看到的屏幕截图吗?我复制粘贴了你的布局,底部和右侧边距都被保留了。 - Alex Townsend
将您的布局xml复制粘贴到测试项目中,并使用填充了TextView的LinearLayout填充ScrollView,得到了这个结果(http://i.imgur.com/zXH0TpK.png)。我猜测您的布局或代码中还有其他未包含在此处的内容导致了边距的更改。您是否在视图层次结构中使用了带有app:layout_behavior的CoordinatorLayout? - Alex Townsend
但是对于ScrollView,我正在添加一个垂直线性布局,并在此布局中添加许多具有此复选框和文本视图的水平线性布局。 - nic2307
非常感谢!但这是改变布局的好方法,对吧? - nic2307
这完全取决于你在做什么。如果你要完全更改布局到一个新的视图,最好使用一个新的ActivityFragment。如果你只是想改变ScrollView中的项目列表,你应该考虑使用ListViewRecyclerView - Alex Townsend

1
将此视图添加到布局底部。

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

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