BottomNavigationView覆盖的RecyclerView

我尝试了谷歌支持库BottomNavigationView与我的片段Framelayout。

这是我的代码

<?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.bottombarnavigation.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <include layout="@layout/toolbar"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.BottomNavigationView android:background="#fcfcfc" android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|end" app:menu="@menu/bottom_navigation" /> </android.support.design.widget.CoordinatorLayout> 

当我在片段内填充我的RecyclerView时,它的内容被BottomNavigationView覆盖。

在这里输入图像描述

我不知道为什么会发生这种情况。 我看通过其他人教程,它工作正常。

编辑这里是我的content_main.xml文件

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="@android:color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.bottombarnavigation.MainActivity" tools:showIn="@layout/activity_main"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </RelativeLayout> 

在这里,我的解决方案,为我工作。

我有几乎相同的布局,我把BottomNavigationView移出CoordinatorLayout因为我不需要任何动画。 我已经将BottomNavigationView对齐到父级的底部,并将layout_above添加到CoordinatorLayout ,使其位于BottomNavigationView之上,但填充所有屏幕。

有了这个配置我解决了重叠的问题,我希望这会帮助你。

在这里你有我的布局。

  <RelativeLayout 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=".ui.activities.MainActivity"> <android.support.design.widget.CoordinatorLayout android:id="@+id/main_coordinator" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:layout_above="@+id/dashboard_navigation"> <android.support.design.widget.AppBarLayout android:id="@+id/main_appbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:elevation="16dp"> <android.support.v7.widget.Toolbar android:id="@+id/dashboard_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary"/> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/main_frame_layout" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/dashboard_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/colorPrimaryDark" app:itemTextColor="@color/colorAccent" app:menu="@menu/menu_main"/> </RelativeLayout> 

您可以将RecyclerView和BottomNavigationView放在LinearLayout中,然后将LinearLayout放在CoordinatorLayout中。 将RecyclerView的属性设置为layout_height="0dp" layout_weight="1" ,将BottomnavigationView的属性设置为layout_height="wrap_content" layout_gravity="bottom"

这是我的代码的一部分,希望能帮助你。

 <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/manager_main_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v4.widget.SwipeRefreshLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout> 

在dp中为BottomNavigationView提供一些静态高度,而不是wrap_content,因为您的父级布局i,e协调器布局扩展了framelayout,其默认行为是将其子视图置于另一个之上。 这是你的碎片容器被botomnavigationview覆盖。

设置回收视图或任何视图的高度为0dp和重量1.这将使其占用所有可用空间。

  1. 将您的BottomNavigationView移至content_main.xml ,并将其放置在RelativeLayout
  2. 将属性android:layout_alignParentBottom="true"BottomNavigationView
  3. 将属性android:layout_above="@id/bottom_navigation"到容器FrameLayout

更新您的布局XML的如下所示:

activity_main.xml中:

 <?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.bottombarnavigation.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <include layout="@layout/toolbar"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout> 

content_main.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="@android:color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.bottombarnavigation.MainActivity" tools:showIn="@layout/activity_main"> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#fcfcfc" app:menu="@menu/bottom_navigation" /> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/bottom_navigation" /> </RelativeLayout> 

希望这将有助于〜

您可以添加一个ItemDecorator到您的回收站视图,添加一些填充。 我使用的是Kotlin而不是Java,但总体思路是:

  recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() { override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) { // Get the position of the view in the recycler view val position = parent?.getChildAdapterPosition(view) if (position == null || position == RecyclerView.NO_POSITION) { return } if (position == parent.adapter.itemCount - 1) { // Add padding to the last item. You should probably use a @dimen resource. outRect?.bottom = 200 } } }) 

在您的布局中包含的主要内容。 给保证金底部的回收者视图。 因为回收站视图隐藏在底部导航视图的后面