DrawerLayout搭配Toolbar作AppBar时RecyclerView内容遮挡问题求助
解决RecyclerView分类标题被Toolbar遮挡的问题
看起来你的布局层级出了点小问题,导致RecyclerView的内容跑到Toolbar下面去了。咱们一步步来修复:
核心问题分析
- 你给
DrawerLayout加了android:layout_below="@id/toolbar",但这个属性是RelativeLayout专属的,DrawerLayout并不支持它,而且toolbar是DrawerLayout的子View,父布局没法引用子View的id来设置位置,这个属性完全无效。 - Toolbar被嵌套在
content_frame的FrameLayout里面,加上内层ConstraintLayout的paddingTop可能因为嵌套层级的问题没生效,导致RecyclerView直接顶到了屏幕顶部。
方案一:用CoordinatorLayout(推荐,符合Material Design)
这是最规范的写法,能自动处理内容和Toolbar的位置关系,还能支持滚动时Toolbar隐藏的效果:
<?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" android:fitsSystemWindows="true"> <!-- 主内容区域 --> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Toolbar 容器 --> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:titleTextColor="@color/colorWhite" app:layout_scrollFlags="scroll|enterAlways"/> <!-- 可选:滚动时隐藏Toolbar --> </android.support.design.widget.AppBarLayout> <!-- 可刷新的内容区域 --> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiper" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- 关键:让内容避开Toolbar --> <android.support.v7.widget.RecyclerView android:id="@+id/lista" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="true"/> </android.support.v4.widget.SwipeRefreshLayout> </android.support.design.widget.CoordinatorLayout> <!-- 侧边导航栏 --> <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/drawer_header" app:menu="@menu/drawer_viewer"/> </android.support.v4.widget.DrawerLayout>
方案二:用LinearLayout简化布局(快速修复)
如果不想用CoordinatorLayout,直接用垂直LinearLayout来排列Toolbar和内容区域,简单粗暴有效:
<?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" android:fitsSystemWindows="true"> <!-- 主内容区域:垂直排列Toolbar和内容 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:titleTextColor="@color/colorWhite"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiper" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/lista" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="true"/> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout> <!-- 侧边导航栏 --> <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/drawer_header" app:menu="@menu/drawer_viewer"/> </android.support.v4.widget.DrawerLayout>
最后别忘了Activity里的配置
确保你在Activity中正确把Toolbar设置为ActionBar,这样左上角的菜单按钮才能正常工作:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawerLayout = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(toggle); toggle.syncState(); }
这样修改后,RecyclerView的分类标题就不会被Toolbar遮挡了,内容会自动从Toolbar下方开始显示~
内容的提问来源于stack exchange,提问作者Luis Abreu
相关产品推荐
相关产品推荐

