如何将RecyclerView正确置于布局底部且位于Bottom Navigation上方
看起来你遇到的核心问题是ConstraintLayout和RelativeLayout属性混用,以及没有正确处理Bottom Navigation和ViewPager的层级关系,导致RecyclerView位置异常。我来一步步帮你修正:
1. 修正地图页面的ConstraintLayout布局
你原来的FrameLayout用了RelativeLayout的android:layout_alignParentBottom="true",但ConstraintLayout不识别这个属性,完全起不到作用。直接把RecyclerView放在ConstraintLayout里,用它的约束规则定位:
<android.support.constraint.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:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="showresultactivity.SlideTabsActivity"> <com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintBottom_toBottomOf="@+id/mapView" app:layout_constraintEnd_toEndOf="@+id/mapView" app:layout_constraintStart_toStartOf="@+id/mapView" app:layout_constraintTop_toTopOf="@+id/mapView" /> <!-- 直接添加RecyclerView,用ConstraintLayout约束定位到底部 --> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerViewInfoWindowMarker" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FCFCFC" <!-- 约束到父布局底部 --> app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" <!-- 预留Bottom Navigation的高度,避免被覆盖 --> android:layout_marginBottom="?attr/actionBarSize" /> </android.support.constraint.ConstraintLayout>
2. 简化RecyclerView的单独布局
你原来的RecyclerView外层套了不必要的RelativeLayout,直接简化成:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/recyclerViewInfoWindowMarker" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FCFCFC" />
3. 修正主布局中Bottom Navigation的层级
你的主布局里,Bottom Navigation可能会覆盖ViewPager的内容,需要给它设置锚点,让它固定在ViewPager底部:
修改主布局的include部分:
<include layout="@layout/bottom_navigation_view" android:id="@+id/bottom_nav_view" app:layout_anchor="@id/container" app:layout_anchorGravity="bottom" />
同时简化bottom_navigation_view布局,去掉多余的RelativeLayout:
<android.support.design.widget.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/white_grey_border_bottom" app:menu="@menu/bottom_navigation_main" />
关键注意点
- ConstraintLayout只能用自身的约束属性:比如
app:layout_constraintBottom_toBottomOf,不要混用RelativeLayout的layout_alignParentBottom这类属性,否则完全无效。 - 预留BottomNav高度:通过
android:layout_marginBottom给RecyclerView留出Bottom Navigation的空间,避免内容被遮挡。如果?attr/actionBarSize不符合你的BottomNav高度,可以换成具体的dp值(比如56dp)。 - 减少布局嵌套:多余的RelativeLayout会增加布局层级,影响渲染性能,能简化就尽量简化。
这样修改后,RecyclerView就会稳定地显示在地图底部、Bottom Navigation上方了。
内容的提问来源于stack exchange,提问作者grap
相关产品推荐
相关产品推荐

