我CoordinateLayout
在片段的布局文件中使用。我需要在两个布局之间使用FloatingActionButton
or 。ImageButton
我从这个答案中得到了示例代码。
我用两个相同的布局文件创建了两个片段。片段1.xml,片段2.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
片段 2
我不明白为什么?任何人都可以帮助我吗?