浮动动作按钮重力不起作用

Kæm*_*ker 2 xml android floating-action-button

我有一个浮动动作按钮应位于右下角,但显然重力不起作用.它显示在左上角.这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rl"
            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">

<ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@mipmap/ic_action_send"
    app:backgroundTint="@color/colorPrimaryDark"
    app:layout_anchor="@id/iv"
    app:layout_anchorGravity="bottom|right|end"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Bla*_*elt 9

我有一个浮动动作按钮应位于右下角,但显然重力不起作用

这是预期的行为,除非你将你的内容包装在一个CoordinatorLayout.例如

<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"
    android:fitsSystemWindows="true">


     <ImageView
      android:id="@+id/iv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

 <android.support.design.widget.FloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom|end"
       android:layout_margin="16dp"
       android:src="@mipmap/ic_action_send"
       app:backgroundTint="@color/colorPrimaryDark"
       app:layout_anchor="@id/iv"
       app:layout_anchorGravity="bottom|right|end"/>
 </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

应该这样做.你可以在这里阅读更多相关信息 CoordinatorLayout