BottomNavigationView with FloatingActionButton ins

2019-07-07 17:07发布

问题:

I want to hide my bottom bar on scroll but a FAB should stay on the screen.

If I put the FAB on top of BottomNavigationView using anchors but it appears behind it.

If I put layout_insetEdge="bottom" to the BottomNavigationView then it works but make my tests fail (https://issuetracker.google.com/issues/70162122) so I cannot use that at the moment.

Layout:

<?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/mainLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=".screens.main.MainActivity">

  <FrameLayout
    android:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/scrolling_view_behaviour" />

  <android.support.design.widget.FloatingActionButton
    android:id="@+id/mainNewQuestionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/spacing_8dp"
    app:layout_anchor="@+id/mainBottomNavigationView"
    app:layout_anchorGravity="top|right|end"
    app:srcCompat="@drawable/create_question"
    tools:visibility="visible" />

  <android.support.design.widget.BottomNavigationView
    android:id="@+id/mainBottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/my_gray"
    app:itemIconTint="@drawable/selector_bottombar_item"
    app:itemTextColor="@drawable/selector_bottombar_item"
    app:layout_anchor="@id/mainContainer"
    app:layout_anchorGravity="bottom"
    app:layout_behavior="@string/hide_bottom_navigation_view_behaviour"
    app:menu="@menu/bottom_navigation_main" />

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

回答1:

You seem to be having a Z-Ordering issue here. You could try the fab.bringToFront() followed by fab.invalidate() for api's below 21, and ViewCompat.setZ(fab, someFloat) for api's above 21.

I used these methods to bring a custom FloatingActionMenu, consisting of a series of FAB's I wrote for a project, to the front so they would overlap everything on the screen.