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

如何让enableEdgeToEdge在Navigation Drawer Views Activity示例中生效?

解决步骤

1. 移除AppBarLayout的适配属性

默认Navigation Drawer模板里的AppBarLayout会带有fitsSystemWindows="true"属性,这个属性会强制布局避开状态栏区域,导致无法实现重叠效果。你需要删掉这个属性:

<com.google.android.material.appbar.AppBarLayout
    android:background="?attr/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.MyApplication.AppBarOverlay"
    <!-- 移除这行:android:fitsSystemWindows="true" -->
    >

2. 调整Toolbar内容位置

去掉fitsSystemWindows后,Toolbar里的标题等内容会被状态栏遮挡,需要给Toolbar添加顶部内边距来避开状态栏:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:paddingTop="?attr/statusBarHeight"
    app:popupTheme="@style/Theme.MyApplication.PopupOverlay" />

也可以用Material组件专属属性,更贴合设计规范:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:contentInsetTop="?attr/statusBarHeight"
    app:popupTheme="@style/Theme.MyApplication.PopupOverlay" />

3. 确认enableEdgeToEdge()调用逻辑

你代码里enableEdgeToEdge()放在super.onCreate(savedInstanceState)之前是符合官方规范的,无需调整,但要确保后续布局初始化流程正确:

override fun onCreate(savedInstanceState: Bundle?) {
    enableEdgeToEdge()
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    
    // 后续AppBarConfiguration、NavigationView等初始化代码保持不变
}

额外提示

如果状态栏文字颜色和AppBarLayout背景冲突(比如浅色背景配白色文字看不清),可以添加代码调整状态栏图标颜色:

ViewCompat.getWindowInsetsController(window.decorView)?.let { controller ->
    controller.isAppearanceLightStatusBars = true // true为深色图标,false为白色图标
}

内容的提问来源于stack exchange,提问作者Cheok Yan Cheng

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:15:13