CoordinateLayout app:layout_anchor和app:layout_anchorGravity无法在片段中工作

问题描述 投票:3回答:1

我正在片段的布局文件中使用CoordinateLayout。我需要在两个布局之间使用FloatingActionButtonImageButton。我从this answer获得了示例代码。

我用两个相同的布局文件创建了两个片段。 fragment1.xml,fragment2.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/viewA"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:background="@android:color/holo_purple"
        android:orientation="horizontal"/>

    <LinearLayout
        android:id="@+id/viewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:background="@android:color/holo_orange_light"
        android:orientation="horizontal"/>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/icon_add_new"
    app:layout_anchor="@id/viewA"
    app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>

[当我运行应用程序时,将显示fragment1,然后在单击按钮后显示fragment2。但是,当用第二个片段替换第一个片段时,按钮位置将变为如下所示的左上角。

片段1

Fragment1

片段2

enter image description here

我不明白为什么?有人可以帮助我吗?

android android-fragments layout coordinate layout-anchor
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.