You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:内容无法显示在AppBar下方的问题求助

解决内容被AppBar遮挡的问题

兄弟,我看你这是典型的内容被AppBar挡住的问题,大概率是布局结构没搭对。你用了DrawerLayout,但主内容区域没和AppBar做好适配,我给你一套标准的布局结构,你照着调整下应该就能解决:

<?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">

        <!-- AppBar部分 -->
        <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>

        <!-- 你的实际内容布局,比如FrameLayout/RecyclerView等 -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            <!-- 这个属性是关键!让内容自动避开AppBar -->
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </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/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

几个关键要点:

  • 主内容区域必须用CoordinatorLayout包裹,它能和AppBarLayout联动,处理内容的偏移
  • 给你的内容布局加上app:layout_behavior="@string/appbar_scrolling_view_behavior",这个属性会自动让内容处于AppBar的下方,哪怕AppBar有滚动隐藏的效果也能适配
  • 确保DrawerLayout和NavigationView都设置了android:fitsSystemWindows="true",避免状态栏区域的适配问题

如果你的项目里不想用CoordinatorLayout,也可以用简单粗暴的方法:给主内容布局加上android:layout_marginTop="?attr/actionBarSize",但这种方法只能固定避开AppBar的高度,没法支持AppBar的滚动交互,优先推荐第一种方案哦。

内容的提问来源于stack exchange,提问作者Moussa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:56:27