问题
When I switch from any fragment back to Shopping List with the bottom app bar, it gets pushed down off the screen and I don't know why. Switching between the Fridge Fragment and Pantry Fragment works great, but switching from either of those back to the Shopping List Fragment pushes the bar off the screen, as shown here:
Github repository with full code
回答1:
Try with these changes for FrameLayout and I guess you won't need fragment_navigation.xml after that.
activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/relative_layout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
app:backgroundTint="@color/colorPrimary"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/ic_menu_white_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_shopping_cart_white_24dp"
app:fabSize="normal"
app:layout_anchor="@id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
You also said that it happens when you switch to ShoppingListFragment which has another CoordinatorLayout in there.
You simply change fragment_shopping_list.xml codes as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shopping_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="141dp"
android:text="TextView" />
</RelativeLayout>
Or just don't use CoordinatorLayout in there then try.
来源:https://stackoverflow.com/questions/52935795/bottomappbar-being-pushed-off-screen