CoordinatorLayout 中 BottomNavigationView 覆盖的 Recyclerview
Recyclerview covered by BottomNavigationView in CoordinatorLayout
我尝试 Google 支持库 BottomNavigationView 和 Fragments 的框架布局。
这是我的代码
<?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>
当我在 Fragment 中填充我的 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>
在 dp 中为 BottomNavigationView 而不是 wrap_content 提供一些静态高度,因为您的父布局,即协调器布局扩展了框架布局,其默认行为是将其子视图一个放在另一个之上。那就是你的片段容器被 botomnavigationview 覆盖了。
设置回收视图或其所在的任何视图,高度为 0dp,重量为 1。这将使其占用所有可用的 space。
这是我的解决方案,对我有用。
我的布局与您几乎相同,我将 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>
- 将您的
BottomNavigationView
移至 content_main.xml
并将其放入 RelativeLayout
- 将属性
android:layout_alignParentBottom="true"
添加到 BottomNavigationView
- 将属性
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>
希望对你有所帮助~
您可以将项目装饰器添加到您的 recyclerview 中,以添加一些填充。我正在使用 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
}
}
})
可以将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>
在您的布局中包含的主要内容。将边距底部提供给回收站视图。
因为回收站视图隐藏在底部导航视图后面
CoordinatorLayout
最有用的功能之一是视图躲避。
CoordinatorLayout
的子视图可以指定为 "insetting" 一条边。
您指定为躲避相同边缘的任何其他子视图将被调整以适应。
在您的情况下,您可以执行以下操作:
<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"
app:layout_dodgeInsetEdges="bottom" /> <-- Specifies this view dodges any views that inset the bottom edge
<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"
app:layout_insetEdge="bottom" /> <-- Specifies that this view insets the bottom edge
</android.support.design.widget.CoordinatorLayout>
我有一个简单的解决方案,可能不是最好的,但它可以完成工作,只需创建一个高度为应用栏(工具栏)高度的视图,应用栏的高度 actionBarSize
和 bottomnav 是一样的,所以你可以将你的 recyclerview 限制在这个视图的顶部,这样做它不会被你的 bottomnav
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.pedidos.PedidoFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/view3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试 Google 支持库 BottomNavigationView 和 Fragments 的框架布局。
这是我的代码
<?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>
当我在 Fragment 中填充我的 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>
在 dp 中为 BottomNavigationView 而不是 wrap_content 提供一些静态高度,因为您的父布局,即协调器布局扩展了框架布局,其默认行为是将其子视图一个放在另一个之上。那就是你的片段容器被 botomnavigationview 覆盖了。
设置回收视图或其所在的任何视图,高度为 0dp,重量为 1。这将使其占用所有可用的 space。
这是我的解决方案,对我有用。
我的布局与您几乎相同,我将 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>
- 将您的
BottomNavigationView
移至content_main.xml
并将其放入RelativeLayout
- 将属性
android:layout_alignParentBottom="true"
添加到BottomNavigationView
- 将属性
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>
希望对你有所帮助~
您可以将项目装饰器添加到您的 recyclerview 中,以添加一些填充。我正在使用 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
}
}
})
可以将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>
在您的布局中包含的主要内容。将边距底部提供给回收站视图。 因为回收站视图隐藏在底部导航视图后面
CoordinatorLayout
最有用的功能之一是视图躲避。
CoordinatorLayout
的子视图可以指定为 "insetting" 一条边。
您指定为躲避相同边缘的任何其他子视图将被调整以适应。
在您的情况下,您可以执行以下操作:
<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"
app:layout_dodgeInsetEdges="bottom" /> <-- Specifies this view dodges any views that inset the bottom edge
<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"
app:layout_insetEdge="bottom" /> <-- Specifies that this view insets the bottom edge
</android.support.design.widget.CoordinatorLayout>
我有一个简单的解决方案,可能不是最好的,但它可以完成工作,只需创建一个高度为应用栏(工具栏)高度的视图,应用栏的高度 actionBarSize
和 bottomnav 是一样的,所以你可以将你的 recyclerview 限制在这个视图的顶部,这样做它不会被你的 bottomnav
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.pedidos.PedidoFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/view3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>