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

标签 android xml

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

关于android - 如何让android支持屏幕右下角的FloatingActionButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088453/

相关文章:

xml - 使用 XSLT 添加/插入子元素

xml - 在 Haskell 中解析 XML

javascript - 如何在 Javascript 中写双引号

java - 将 ID 保存到资源文件时出现问题

android - 在 Robolectric 中模拟单击菜单项

Android ImageView.setImageURI 缩放图片

c# - XML读取子节点

android - 实现 onSavedInstanceState 会停止 FragmentStatePagerAdapter 的工作

Android TTS 和口型同步

android - 如果没有 react-native,是否可以在 Android 和/或 iOS 的后台任务中收集地理位置信息?