隐藏在底部导航栏下方的FloatingActionBar

时间:2018-08-06 14:05:19

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

我的片段布局包含一个带有FloatingActionButton的CoordinatorLayout。父活动还包含一个CoordinatorLayout和一个带片段的PageView。我的问题是FAB位于导航栏下方而不是上方。当我滚动相关的RecyclerView时,它也会上下移动,而我希望它保持不变。这是活动的XML布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="uk.ac.aber.dcs.cs31620.faaversion4.ui.FAAMainActivity">

   <android.support.design.widget.CoordinatorLayout

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

       <android.support.design.widget.AppBarLayout
           android:id="@+id/appBarLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">

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

           <android.support.design.widget.TabLayout
               android:id="@+id/tabs"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               app:tabMode="scrollable" />

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

       <android.support.v4.view.ViewPager
           android:id="@+id/pager"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemTextColor="?android:textColorPrimary"
    app:itemIconTint="?colorPrimaryDark"
    app:menu="@menu/menu_nav" />
</android.support.v4.widget.DrawerLayout>

以下是工具栏的代码:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:background="?attr/colorPrimary"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:layout_scrollFlags="scroll|enterAlways"/>

最后,这是片段布局的代码:

<?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:id="@+id/coordinator"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="uk.ac.aber.dcs.cs31620.faaversion4.ui.cats.CatsFragment">

   <android.support.constraint.ConstraintLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:columnCount="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Spinner
            android:id="@+id/breeds_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="50"
            android:layout_row="0" />

        <Spinner
            android:id="@+id/gender_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="50"
            android:layout_row="0" />

        <Spinner
            android:id="@+id/age_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="50"
            android:layout_row="1" />

        <TextView
            android:id="@+id/proximity_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="50"
            android:layout_row="1"
            android:text="@string/distance"
            android:textAppearance="@style/MyTextAppearance" />

    </GridLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cat_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/gridLayout" />
   </android.support.constraint.ConstraintLayout>

   <android.support.design.widget.FloatingActionButton
       android:id="@+id/fab_add_cat"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginEnd="8dp"
       android:src="@drawable/ic_add_white_24dp"
       android:layout_gravity="bottom|end" />

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

作为工作回合,我在64dp的FAB上放置了一个layout_MarginBottom,尽管它显示了它,但随着选项卡栏的折叠,它仍然上下移动。另外,生成的小吃栏也隐藏在底部导航栏的下方。我所看到的所有示例在活动级别都具有FAB和协调器布局,并且没有嵌套其他协调器布局。也许唯一的选择是将FAB放在活动布局中并从片段中访问它(例如,在不需要时将其隐藏)?谢谢。

1 个答案:

答案 0 :(得分:0)

如果您要使用ConstraintLayout,则应考虑利用其功能,以便您的UI采取相应的行动。 ConstraintLayouts帮助您删除小部件和布局的嵌套。它使它们更平整,更具可读性,减少了代码,并使您的应用程序更流畅。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     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="uk.ac.aber.dcs.cs31620.faaversion4.ui.FAAMainActivity">

    <Spinner
        android:id="@+id/breeds_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/gender_spinner"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/gender_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/breeds_spinner"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/age_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/gender_spinner"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/breeds_spinner" />

    <TextView
        android:id="@+id/proximity_button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="@string/distance"
        android:textAppearance="@style/MyTextAppearance"
        app:layout_constraintBottom_toBottomOf="@id/age_spinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/age_spinner"
        app:layout_constraintTop_toBottomOf="@id/gender_spinner" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cat_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/age_spinner" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_cat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:src="@drawable/ic_add_white_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>