Android FloatingActionButton位于错误的位置

时间:2016-11-15 13:11:19

标签: android position floating-action-button

我使用下面的代码作为RecyclerView中的项目布局,FloatingActionButton应该位于底端角落,但它有时出现在top-start上,我想弄清楚为什么会发生这种情况......

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="@dimen/record_task_card_width"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/record_task_card_margin"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <FrameLayout
                android:id="@+id/card_background_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="@drawable/bg_card">

                <ImageView
                    android:id="@+id/record_task_icon"
                    android:layout_width="@dimen/record_task_icon_size"
                    android:layout_height="@dimen/record_task_icon_size"
                    android:layout_gravity="center"
                    android:contentDescription="@string/app_name"/>

            </FrameLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/record_task_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/record_task_info_margin"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                <TextView
                    android:id="@+id/record_task_caption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/TextAppearance.AppCompat.Caption"/>

            </LinearLayout>

        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/start_record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:clickable="true"
            app:layout_anchor="@id/card_background_container"
            app:layout_anchorGravity="bottom|end|right"
            app:srcCompat="@drawable/ic_start_record"/>

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

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

1 个答案:

答案 0 :(得分:3)

删除

 app:layout_anchor="@id/card_background_container"
 app:layout_anchorGravity="bottom|end|right"

intead add

android:layout_gravity="bottom|end" 

样品:

<android.support.design.widget.FloatingActionButton
            android:id="@+id/start_record_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:clickable="true"
            android:layout_gravity="bottom|end"
            app:srcCompat="@drawable/ic_start_record"/>