【问题标题】:Android FloatingActionButton locates in wrong positionAndroid FloatingActionButton 位置错误
【发布时间】:2016-11-15 13:11:19
【问题描述】:

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

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

【问题讨论】:

  • 删除app:layout_anchor="@id/card_background_container"
  • 为浮动操作按钮添加 android:layout_gravity="bottom|end|right" 并删除 app:layout_anchor="@id/card_background_container"

标签: android position floating-action-button


【解决方案1】:

删除

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

添加

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

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 2019-07-13
    • 2015-11-30
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2012-03-23
    相关资源
    最近更新 更多