本文介绍了如何使android支持屏幕右下角的FloatingActionButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have added a FloatingActionButton to my layout inside a RelativeLayout as follow

<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 的位置没有更改,它停留在左上方.

As you see, I have set layout_gravity to bottom|right but the place of my FloatingActionButton hasn't changed and it rests on top left.

如何在右下角设置 FloatingActionButton ?

推荐答案

使用CoordinatorLayout:

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

这篇关于如何使android支持屏幕右下角的FloatingActionButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 03:07