FAB按钮位置错误

时间:2015-12-09 18:55:40

标签: android user-interface layout material-design floating-action-button

我一直在努力争取我的FAB的位置,此时,由于一个未知的原因(对我来说),按钮位于左上方,当它被调整到最低点时。

这是我的代码:

<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"
android:background="@color/l_gray"
android:weightSum="1">

<ListView
    android:id="@+id/lv_vehiculos"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_margin="0dp"
    android:divider="@color/transparent"
    android:dividerHeight="15dp"
    android:layout_weight="0.83"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:layout_marginBottom="0dp"
    android:clickable="true"
    android:src="@drawable/icon_white_plus"
    app:backgroundTint="@color/s_gray"
    app:borderWidth="0dp"
    app:elevation="2dp"
    android:id="@+id/view" />

这就是结果。

FAB missplaced

3 个答案:

答案 0 :(得分:0)

相对布局及其子窗口不能使用布局权重和权重属性及其行为。相对布局的直接孩子也没有布局重力属性,所以你期望从这些属性的任何行为都不会起作用

因此,为了使工厂位于右下方,您需要使用相对布局中可用的定位属性,以便通过将其值设置为{{1}来启用FAB的android:layout_alignParentBottom属性然后按钮将移动到底部,然后如果您还通过将其设置为true来启用FAB的android:layout_alignParentRight属性,则该按钮将粘贴到父相对布局的右端,因此您将您的按钮成功定位到relativelayout(父级)的右下角。

但是在你的情况下,一个更简单的解决方案就是将root relativelayout更改为linearlayout,因为我可以在你的xml中清楚地知道你的根布局的子项是为linearlayout父设置的,并且你天真地复制了将该代码粘贴到具有根相对布局的布局

答案 1 :(得分:0)

如果您使用RelativeLayout作为父级,那么您应该使用以下内容将其发送到视图的右下角:

android:alignParentRight="true"

android:alignParentEnd="true"

android:alignParentBottom="true"

答案 2 :(得分:0)

您的布局存在一些问题。

您可以通过多种方式实现布局:

  • 更好:使用CoordinatorLayoutandroid:layout_gravity="end|bottom"。并且可以使用RecyclerView代替ListView
  • 使用LinearLayout代替RelativeLayout
  • RelativeLayoutandroid:alignParentEnd="true"android:alignParentBottom="true"
  • 一起使用
  • 如果您想使用体重,可以使用新的PercentRelativeLayout
相关问题