【问题标题】:FAB button in wrong positionFAB 按钮位置错误
【发布时间】:2016-03-15 04:44:38
【问题描述】:

我一直在为我的 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" />

这就是结果。

【问题讨论】:

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


    【解决方案1】:

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

    android:alignParentRight="true"

    android:alignParentEnd="true"

    android:alignParentBottom="true"

    【讨论】:

      【解决方案2】:

      相对布局及其子布局不能使用布局权重和 weightsum 属性及其行为。相对布局的直接子级也没有布局重力属性,因此您期望从这些属性中获得的任何类型的行为都不会起作用

      因此,为了使工厂位于右下角,您需要使用可在相对布局中使用的定位属性,因此,如果您要通过将其值设置为 true 来启用 FAB 的 android:layout_alignParentBottom 属性,那么按钮将移动到底部,然后如果您还通过将 FAB 设置为 true 来启用 android:layout_alignParentRight 属性,那么按钮将粘在父相对布局的右端,因此您成功地将按钮定位在右下角你的相对布局(父母)。

      但在您的情况下,一个更简单的解决方案是将根 relativelayout 更改为 linearlayout,因为我可以在您的 xml 中很清楚,您的根布局的子级是为线性布局父级设置的,而您天真地复制将该代码粘贴到具有根相对布局的布局中

      【讨论】:

        【解决方案3】:

        您的布局存在一些问题。

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

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

        【讨论】:

          猜你喜欢
          • 2016-12-12
          • 1970-01-01
          • 2020-02-01
          • 1970-01-01
          • 2015-12-26
          • 1970-01-01
          • 1970-01-01
          • 2017-04-22
          • 2019-11-20
          相关资源
          最近更新 更多