如何让android支持屏幕右下角的FloatingActionButton



我在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

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

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

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

希望这能有所帮助。

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

添加此:
app:layout_anchorGravity="bottom|right|end"

从代码中清除:

android:layout_gravity="bottom|right"

添加此行:

app:layout_anchorGravity="bottom|right|end"

最新更新