【问题标题】:FloatingActionBar hidden under bottom navigation bar隐藏在底部导航栏下的 FloatingActionBar
【发布时间】:2019-01-13 12:34:48
【问题描述】:

我的片段布局包含一个带有 FloatingActionButton 的 CoordinatorLayout。父活动还包含一个带有 PageView 的 CoordinatorLayout,它会引入片段。我遇到的问题是 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,但尽管它显示它仍然在标签栏折叠时上下移动。此外,生成的 Snackbar 也隐藏在底部导航栏下方。我见过的所有示例都在活动级别具有 FAB 和协调器布局,并且没有嵌套进一步的协调器布局。也许唯一的选择是将 FAB 放在活动布局中并从片段中访问它(例如,在不需要时隐藏它)?谢谢。

【问题讨论】:

  • 尝试使用 android:clipToPadding=”false”

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


【解决方案1】:

如果您打算使用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>

【讨论】:

  • 您没有遇到问题。因为片段的容器和底部导航是相同的布局,并且他的工厂在片段内,所以工厂在导航视图下。我的建议是添加使用约束,但在活动中,而不是在片段内
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 2021-08-25
  • 1970-01-01
相关资源
最近更新 更多