【问题标题】:Position of second Floating Action Button isn't locked in layout第二个浮动操作按钮的位置未锁定在布局中
【发布时间】:2015-09-08 17:45:33
【问题描述】:

我有一个包含两个 CardView 的布局。每个人的右下角都有一个 FAB。 POrtrait 工作正常,但在 Landscape 中,第二个 CardView 的 FAB “脱离”并随着布局滚动。你知道我的错误在哪里吗?

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

【问题讨论】:

  • 你创建了横向布局,对吗?

标签: android layout scroll floating floating-action-button


【解决方案1】:

首先请记住,您将第一个CardView 命名为cv2,将第二个命名为cv。因此,有问题的按钮是您的 xml 中的第一个,而不是第二个。

稍后,app:layout_behavior="@string/appbar_scrolling_view_behavior" 表示您希望您的 NestedScrollView 子视图自动滚动。现在,由于您的按钮位置取决于NestedScrollViews 的其中一个孩子,请检查这些是否会导致问题。

另外,您可能希望在您的CardView 中让FAB 成为您的LinearLayout 的子代。当方向改变时,会重新创建活动/片段,并且重新初始化所有布局参数,并且可能会出现中断。

另外,看看类似的问题:

Floating Action Button not showing fully inside a fragment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多