FAB 未使用 RecyclerView 定位在 CoordinatorLayout 内

FAB not positioning inside CoordinatorLayout with RecyclerView

我正在尝试显示带有 FAB 的项目列表以添加新项目。当前画面如下。

我有一个只有 RecyclerView 和 FAB 的 CoordinatorLayout。我的问题是 FAB 没有锚定到页面底部,而是锚定到 RecyclerView 的底部,其高度似乎表现得好像它的 wrap_content,但我声明为 match_parent。 RecylerView 有一个银色背景用于说明目的。

<android.support.design.widget.CoordinatorLayout
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="com.ae.apps.tripmeter.fragments.expenses.TripsListFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/silver"
    android:layout_margin="@dimen/activity_horizontal_margin"
    app:layoutManager="LinearLayoutManager"
    tools:listitem="@layout/fragment_trip"/>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add"
    app:elevation="8dp"/>

我不明白为什么我无法将 FAB 定位在页面底部。

尝试将 RecyclerView 放入 'content' 布局文件中,然后通过

将其添加到主布局中
<include layout="@layout/content_recyclerview" />

编辑:

如果要将 FAB 放置在 Activity 的底部,则不应将其放置在 Fragment 中。从片段中移除 FAB 并将其添加到主 activity。像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="com.your_company.your_project">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <-- ref. your recycler here -->
    <include layout="@layout/content_with_your_list"/>

    <-- Put the FAB here -->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
    />

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

现在在您的片段中删除 FAB!它应该与 CoordinateLayout 一起位于主 activity 中。在片段中使用简单的 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              tools:showIn="@layout/activity_your_main_activity"
              >


    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/silver"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layoutManager="LinearLayoutManager"/>


</LinearLayout>

您需要进行适当的更改以适合您的项目。


切换 FAB 可见性

为了切换 FAB 的可见性,将以下 属性 添加到您父级 Activity 中的 FloatingActionButton

android:visibility="gone"

并使用以下代码使其在代码中可见:

mFab.setVisibility(View.VISIBLE);

或不可见,再次使用

mFab.setVisibility(View.GONE);

只要你需要,例如。当显示或删除片段时。

你可能想要这样的东西?

Example

<android.support.design.widget.CoordinatorLayout
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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|end"
    android:src="@android:drawable/ic_input_add"
    android:layout_margin="8dp"
    app:elevation="8dp"/>

如果你想在fab下有一些布局,你应该使用相对布局作为根布局。并将我的代码放在这个相对布局中。

我使用以下方法将 fab 锚定在 recyclerView 的底部,它为我提供了我想要的输出。试试这个..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

<android.support.design.widget.FloatingActionButton
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_margin="16sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</RelativeLayout>