Android中RecyclerView被Toolbar遮挡问题求助
Hey Adrian, let's fix that Toolbar-overlapping-RecyclerView issue you're facing with Drawer Layout! This is a super common gotcha when working with these components, so here are a few reliable solutions:
This is the cleanest approach, following Material Design guidelines, and it automatically handles layout positioning so your RecyclerView stays below the Toolbar. Here's how to adjust your activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Main content container --> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Toolbar wrapped in AppBarLayout --> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <!-- Include your content_main with the scrolling behavior --> <include layout="@layout/content_main" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout> <!-- Navigation Drawer --> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"/> </android.support.v4.widget.DrawerLayout>
The magic here is the app:layout_behavior="@string/appbar_scrolling_view_behavior" attribute — it tells the content layout to automatically position itself below the AppBarLayout, eliminating overlap. It also supports cool interactions like hiding the Toolbar when scrolling the RecyclerView later on.
If you don't want to use CoordinatorLayout, you can explicitly push the RecyclerView down to avoid the Toolbar. Open your content_main.xml and add a top margin matching the Toolbar's height:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize"/> </LinearLayout>
Using ?attr/actionBarSize ensures you're matching the system-defined Toolbar height, so it works across different devices and themes. Just note this method doesn't support dynamic Toolbar behaviors like scrolling.
Make sure you haven't misconfigured the fitsSystemWindows attribute. Typically, setting android:fitsSystemWindows="true" on your DrawerLayout and AppBarLayout helps the system calculate status bar spacing correctly, which can prevent unexpected overlaps too.
Pick the solution that fits your project setup — Solution 1 is the most robust for long-term maintainability!
内容的提问来源于stack exchange,提问作者Adrian Lopez

