【问题标题】:How to make android support FloatingActionButton at bottom right of the screen?如何让android支持屏幕右下角的FloatingActionButton?
【发布时间】:2017-09-11 20:26:56
【问题描述】:

我在RelativeLayout 内的布局中添加了FloatingActionButton,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_ok" />

</RelativeLayout>

如您所见,我已将layout_gravity 设置为bottom|right,但我的FloatingActionButton 的位置没有改变,它位于左上方。

我怎样才能让我的FloatingActionButton 在右下角?

【问题讨论】:

    标签: android xml


    【解决方案1】:

    使用 CoordinatorLayout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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">
    
    <RelativeLayout
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </RelativeLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:layout_margin="10dp"
        android:id="@+id/myFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#FF0000"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/test"
        app:layout_anchorGravity="bottom|right|end" />
    </android.support.design.widget.CoordinatorLayout>
    

    注意

       app:layout_anchor="@id/test"
       app:layout_anchorGravity="bottom|right|end"
    

    【讨论】:

    • CoordinatorLayout 被用于处理这些情况。如果我在你里面,我会坚持下去@DäñishShärmà
    • 我已经用你的答案编辑了我的代码,我赞成你的答案。还有一件事我想问如何更改浮动按钮的背景颜色。我想让它和我的 colorPrimaryDark 一样。有可能吗?
    • 如果有app:backgroundTint="#FF0000",请删除它。默认情况下,如果你的主题设置正确,你应该得到它
    【解决方案2】:

    由于您使用 RelativeLayout 包装 FloatingActionButton,因此请改用以下属性:

    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      从您的代码中清除:
      android:layout_gravity="bottom|right"

      添加这个:
      app:layout_anchorGravity="bottom|right|end"

      【讨论】:

        【解决方案4】:

        从您的代码中清除:

        android:layout_gravity="bottom|right"

        添加这一行:

        app:layout_anchorGravity="bottom|right|end"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-02
          • 2019-09-04
          • 2019-01-05
          • 2012-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多