CoordinateLayout
フラグメントのレイアウト ファイルで使用しています。そこで、2 つのレイアウトの間でFloatingActionButton
orを使用する必要があります。この回答ImageButton
からサンプルコードを取得しました。
2 つの同じレイアウト ファイルで 2 つのフラグメントを作成しました。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が表示されます。ただし、最初のフラグメントを 2 番目のフラグメントに置き換えると、ボタンの位置が以下のように左上隅に変更されます。
フラグメント 1
フラグメント 2
私はなぜ理解できないのですか?誰でも私を助けることができますか?