<Include> 一个 xml 文件,其中包含一个 fab 按钮

<Include> an xml file with a fab button as it is

我想将另一个 xml 文件包含到 xml 文件中,该文件包含与它完全相同的 fab 按钮 is.The 问题是当我 <include layout="@layout/fab 它在屏幕的左上角而不是右下角显示我的 fab 按钮。

这是我的 xml 文件(ConstraintLayout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlanPage.PlanPageView">


<include
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/activity_fab_button"
    android:layout_gravity="end|bottom"/>

</android.support.constraint.ConstraintLayout>

这是很棒的按钮 xml

<android.support.design.widget.FloatingActionButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/ic_plusphoto_fab"
android:backgroundTint="@color/colorPrimary"
/>

fab里面的这个属性xml:

android:layout_gravity="end|bottom"

将布局包含在另一个布局中时无效。
您必须为此布局设置属性:

<include
    layout="@layout/activity_fab_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

如果有效。
如果include布局的宿主布局不是LinearLayout,那么你必须设置其他属性,比如 alignParentBottom 对于 RelativeLayout,或 将其约束到 ConstraintLayout 的 Bottom 和 End。