Android 协调布局中底部导航视图覆盖的回收视图
我尝试了Google Support Library BottomNavigationView,为我的片段提供了一个框架布局 这是我的密码Android 协调布局中底部导航视图覆盖的回收视图,android,android-support-library,android-coordinatorlayout,bottomnavigationview,Android,Android Support Library,Android Coordinatorlayout,Bottomnavigationview,我尝试了Google Support Library BottomNavigationView,为我的片段提供了一个框架布局 这是我的密码 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schem
<?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>
我不知道为什么会这样。我看过别人的教程,效果很好
编辑
这是我的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而不是wrap_内容在dp中提供一些静态高度,因为父布局i,e coordinator布局扩展了framelayout,其默认行为是将子视图一个置于另一个之上。也就是说,您的碎片容器被botomnavigationview覆盖。将回收脉络或其所在的任何视图的高度设置为0dp,重量设置为1。这将占用所有剩余的可用空间 这里是我的解决方案,对我有效
我的布局与您几乎相同,我将底部导航视图
移出了协调布局
,因为我不需要任何动画。
我已将BottomNavigationView
对齐到父级底部,并将layout_添加到CoordinatorLayout
中,使其位于BottomNavigationView
上方,但填充了所有屏幕
通过这种配置,我修复了重叠的问题,我希望这会对您有所帮助
<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>
这是我的布局
<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\u main.xml
并将其放置在RelativeLayout
将属性android:layout\u alignParentBottom=“true”
添加到BottomNavigationView
将属性添加到容器FrameLayout
更新布局XML,如下所示:
活动\u 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>
<?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>
content\u 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>
<?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
}
}
})
您可以将RecyclerView和BottomNavigationView放置在LinearLayout中,然后将LinearLayout放置在CoordinatorLayout中。将RecyclerView的属性设置为layout\u height=“0dp”
layout\u weight=“1”
,将BottomnavigationView的属性设置为layout\u height=“wrap\u content”
layout\u 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>
在布局中包含的主要内容中。将页边距底部指定给“回收者”视图。
因为回收器视图隐藏在底部导航视图的后面所以协调布局最有用的功能之一就是视图回避
坐标布局的子视图可以指定为“插入”边。
然后,将指定为避开该相同边的任何其他子视图调整为适合
在您的情况下,您可以执行以下操作:
<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
的appBar(工具栏)的高度相同,appBar和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>
试试这个。我认为属性app:layou锚和app:layou anchorgavity丢失了。@DmitriyKaluzhin我刚试过,结果是一样的。recyclerview内容仍然落后于BottomNavigationView您找到这个问题的答案了吗?或者为片段容器提供静态高度,或者删除协调器布局,替换为相对布局,并向其子视图添加规则以实现您的UI您可以在include layout行中放置内容\u主布局吗,使其为0dp,重量为1。如果不起作用,则尝试对帧布局/相对布局执行此操作。此操作可解决底部重叠问题,但会导致AppBarLayout的顶部重叠(即使使用了appbar滚动查看行为)@DanieleSegato事实并非如此。将app:layout\u behavior=“@string/hide\u bottom\u view\u on\u scroll\u behavior”
添加到您的BottomNavigationView
并检查发生了什么”)这就是我要找的。另外:底部是默认的布局。如果你想忽略它,没有一个是不起作用的,所以把它设为顶部或其他。应用工具栏时缺少工具栏。有什么想法吗?为我工作!用BottomAppBar和那些设置很好地解决了这个问题。您是否愿意链接到源代码以了解如何执行此操作?如果工具栏使用了app:layout\u scrollFlags=“scroll | enterally”
,则向下滚动时会导致BottomNavigationView
隐藏。这不允许在BottomNavigationView上使用协调器功能,就像在上面移动snackbar和浮动操作按钮浪费了很多时间来解决这个问题一样,最终这样一个简单的技巧解决了这个问题。谢谢分享…很好的解决方案!如果可能的话,你能解释一下如何设置边距而不是填充吗?