Положение второй плавающей кнопки действия не зафиксировано в макете

У меня есть макет с двумя CardViews. У каждого есть FAB внизу справа. POrtrait работает нормально, но в альбомной ориентации FAB второго CardView «отрывается» и прокручивается вместе с макетом. Знаете, где моя ошибка?

<RelativeLayout 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=".MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />


<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


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

            <android.support.v7.widget.CardView
                android:id="@+id/cv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:layout_margin="20dp"
                android:layout_marginTop="50dp">

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

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TableRow>

                            <android.support.v7.widget.SwitchCompat
                                android:id="@+id/switch_persistent"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:textOff=""
                                android:textOn="" />

                            <TextView
                                android:id="@+id/tv_persistent_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/persistent_reminder"
                                android:textSize="@dimen/abc_text_size_large_material" />

                        </TableRow>
                    </TableLayout>

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="15dp"
                        android:paddingTop="10dp"
                        android:text="@string/reminder"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/abc_text_size_large_material"
                        android:textStyle="italic" />

                </LinearLayout>
            </android.support.v7.widget.CardView>


            <android.support.v7.widget.CardView
                android:id="@+id/cv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:layout_margin="20dp"
                android:layout_marginTop="50dp">

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

                    <TableLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TableRow>

                            <android.support.v7.widget.SwitchCompat
                                android:id="@+id/switch_startup"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:textOff=""
                                android:textOn="" />

                            <TextView
                                android:id="@+id/tv_startup_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/startup_reminder"
                                android:textSize="@dimen/abc_text_size_large_material" />


                        </TableRow>

                    </TableLayout>


                    <TextView
                        android:id="@+id/tv_startup_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="15dp"
                        android:paddingTop="10dp"
                        android:text="@string/reminder"
                        android:textColor="@android:color/black"
                        android:textSize="@dimen/abc_text_size_large_material"
                        android:textStyle="italic" />

                </LinearLayout>
            </android.support.v7.widget.CardView>


        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_startup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:src="@drawable/pencil"
        app:backgroundTint="@color/accentColor"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/cv"
        app:layout_anchorGravity="end|bottom"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@android:color/white" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_persistent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:src="@drawable/pencil"
        app:backgroundTint="@color/accentColor"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/cv2"
        app:layout_anchorGravity="end|bottom"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@android:color/white" />

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

Working:

Не работает:


person Nglhrdtptrck    schedule 08.09.2015    source источник
comment
Вы создали ландшафтный макет, верно?   -  person Mariano Zorrilla    schedule 08.09.2015


Ответы (1)


Прежде всего, имейте в виду, что вы назвали первый CardView как cv2, а второй как cv. Поэтому кнопка, с которой возникает проблема, является первой в вашем xml, а не второй.

Позже app:layout_behavior="@string/appbar_scrolling_view_behavior" указывает, что вы хотите, чтобы ваши NestedScrollView дочерние представления автоматически прокручивались. Теперь, поскольку позиции ваших кнопок зависят от одного из дочерних элементов NestedScrollViews, проверьте, не вызывает ли это проблему.

Кроме того, вы можете захотеть, чтобы FAB были дочерними элементами ваших LinearLayout внутри ваших CardView. При изменении ориентации происходит пересоздание активности/фрагмента и переинициализация всех параметров макета и, возможно, там что-то ломается.

Также обратите внимание на похожую задачу:

Плавающая кнопка действия не отображается полностью внутри фрагмента

person George Daramouskas    schedule 08.09.2015