开发应用沉浸式效果
=========

#### 问题现象

游戏类应用需要在整个生命周期内实现沉浸式效果，小说阅读和影音视频类应用需要特定页面实现沉浸式效果，以及沉浸式模式下应用内容如何避让安全区，针对以上需求，目前有哪些比较成熟的方案。

#### 背景知识

*   [startAbility](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inner-application-uiabilitycontext#startability)通过该方法可以启动一个UIAbility。
*   [getMainWindowSync](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-windowstage#getmainwindowsync9)获取该windowStage实例下的主窗口。
*   [setWindowLayoutFullScreen](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setwindowlayoutfullscreen9)设置主窗口或子窗口的布局是否为沉浸式布局，使用Promise异步回调。沉浸式布局生效时，布局不避让状态栏与导航栏，组件可能产生与其重叠的情况。
*   [getWindowAvoidArea](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#getwindowavoidarea9)获取当前应用窗口内容规避的区域。如系统栏区域、刘海屏区域、手势区域、软键盘区域等与窗口内容重叠时，需要窗口内容避让的区域。
*   [on('avoidAreaChange')](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#onavoidareachange9)开启当前应用窗口系统规避区变化的监听。
*   [setSpecificSystemBarEnabled](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setspecificsystembarenabled11)可以设置主窗口状态栏、底部导航区域的显示或隐藏。
*   [setWindowBackgroundColor](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setwindowbackgroundcolor9)设置窗口的背景色。未调用该接口时，窗口在浅色模式默认背景色为'#FFF0F0F0'，在深色模式默认背景色为'#FF1A1A1A'。
*   [expandSafeArea](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-expand-safe-area#expandsafearea)控制组件扩展其安全区域。
*   [background](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-background#background10)设置组件背景。从API version 20开始，content参数新增了对ResourceColor类型的支持，并新增了背景向父组件的安全区扩展的能力。
*   [padding](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-size#padding)设置组件的内边距属性。
*   [setWindowDecorHeight](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setwindowdecorheight11)设置窗口的标题栏高度，对存在标题栏和三键区的窗口形态生效。
*   [setWindowDecorVisible](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#setwindowdecorvisible11)设置窗口标题栏是否可见，对存在标题栏和三键区的窗口形态生效。
*   [setTitleAndDockHoverShown](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#settitleanddockhovershown14)设置主窗口进入全屏模式时鼠标Hover到热区上是否显示窗口标题栏和Dock栏。
*   [maximize](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#maximize12)实现最大化功能。主窗口可调用此接口实现最大化功能；子窗口需在创建时设置子窗口参数maximizeSupported为true，再调用此接口可实现最大化功能。

#### 解决方案

本文通过十种方案实现沉浸式效果，以下表格为十种方案的对比及适用场景。

| 
方案

 | 

关键技术点

 | 

适用场景

 |
| :-- | :-- | :-- |
| 

沉浸式模式下显示避让区。

 | 

在EntryAbility中通过窗口的setWindowLayoutFullScreen方法设置窗口为沉浸式，并用getWindowAvoidArea接口获取状态栏和导航条的高度，保存在AppStorage中，并在页面中获取ShowAvoidAreaEntryAbility.ets中保存的状态栏和导航条的高度，并通过padding给页面设置内边距以达到避让导航栏的效果。

 | 

此方案适用于应用全局设置沉浸式，并在页面内控制是否避让安全区。

 |
| 

页面中设置沉浸式并隐藏避让区。

 | 

在页面中通过setWindowLayoutFullScreen方法设置窗口沉浸式，并通过setSpecificSystemBarEnabled方法来控制状态栏和导航条的显示和隐藏。

 | 

此方案适用应用全局设置沉浸式并隐藏状态栏和导航条，如游戏、儿童识字学习类应用的场景。

 |
| 

通过设置窗口背景色实现沉浸式。

 | 

通过setWindowBackgroundColor设置窗口背景色实现沉浸式效果。

 | 

此方案适用于实现页面级沉浸式，实现简单，适用于页面整体色调一致的场景。

 |
| 

通过expandSafeArea实现沉浸式。

 | 

通过expandSafeArea属性控制组件扩展其安全区域

 | 

此方案适用于实现页面级沉浸式，并且可单独设置状态栏、导航条是否沉浸式。

 |
| 

滚动类组件实现沉浸式。

 | 

使用expandSafeArea属性控制List组件是否扩展到状态栏或者导航条。

 | 

此方案适用于滚动类组件。

 |
| 

通过background实现页面级沉浸式。

 | 

通过background设置页面的背景来实现沉浸式效果。

 | 

此方案实现简单，适用于实现页面级沉浸式。

 |
| 

通过ignoreLayoutSafeArea实现页面级沉浸式。

 | 

通过getWindowAvoidArea获取当前状态栏和导航条的高度，并设置ignoreLayoutSafeArea和组件的高度为LayoutPolicy.matchParent，再通过padding属性设置组件的内边距达到避让的效果。

 | 

作为组件级的沉浸式方案，每个页面均需单独配置。当页面内容与避让区发生冲突时，需由开发者手动进行避让处理。

 |
| 

通过ignoreLayoutSafeArea实现沉浸式并避让挖孔区。

 | 

通过setSpecificSystemBarEnabled方法设置状态栏和导航栏隐藏，最后通过on('avoidAreaChange')方法监听挖孔区的方向，根据此方向，通过padding设置组件的内边距，达到避让挖孔区的效果。

 | 

在需要充分利用屏幕可视区域的场景中，若要在状态栏显示重要信息或操作按钮，不仅需隐藏原始状态栏和导航条，还需对挖孔区域进行避让。这种情况下可以将背景全屏沉浸，内容区对挖孔区域做避让处理。

 |
| 

自由多窗实现全屏和非全屏模式时的沉浸式。

 | 

通过setWindowDecorVisible(false)接口设置标题栏的隐藏，仅保留右上角的三键区，setWindowDecorHeight()接口设置标题栏高度，控制右上角三键区显示高度，on('windowTitleButtonRectChange')注册右上角三键大小变化监听，用于页面布局避让。

 | 

此方案适用于自由窗口模式下，需隐藏默认标题栏，保留关闭、最小化、最大化三键区，使页面延伸至原标题栏区域，实现窗口沉浸式。

 |
| 

自由多窗实现仅在全屏模式时的沉浸式。

 | 

通过maximize接口设置window.MaximizePresentation.ENTER\_IMMERSIVE窗口为全屏模式，通过setTitleAndDockHoverShown接口设置窗口标题栏和Dock栏隐藏即可实现仅在全屏模式时的沉浸式。

 | 

此方案适用于自由窗口模式下仅在全屏模式时的沉浸式。

 |

创建一个页面，作为十种沉浸式方案的入口载体，并创建十个EntryAbility，详细命名见以下表格。

<table id="zh-cn_topic_0000002530313716_table1955766155712"><tbody><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row8609106165713"><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p260912625715">沉浸式模式下显示避让区</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p15609064577">页面中设置沉浸式并隐藏避让区</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p2060910635720">通过设置窗口背景色实现沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p760936125712">通过expandSafeArea实现沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p166097610579">滚动类组件实现沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p5609136175717">通过background实现页面级沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p960914615712">通过ignoreLayoutSafeArea实现页面级沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p06092063573">通过ignoreLayoutSafeArea实现沉浸式并避让挖孔区</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p1760914618578">自由多窗实现全屏和非全屏模式时的沉浸式</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p1860917619574">自由多窗实现仅在全屏模式时的沉浸式</p></td></tr><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row15609196105710"><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p18609126135719">ShowAvoidAreaEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p56097611576">HideAvoidAreaEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p1960996115714">WindowBackGroundColorEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p260910619572">ExpandSafeAreaEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p17609126195717">ScrollableComponentEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p5609768576">PageLevelFullScreenEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p18609060578">PageFullScreenByIgnoreLayoutSafeAreaEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p14609176185715">IgnoreLayoutSafeAreaAvoidCutoutEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p166091469573">FreeWindowFullScreenEntryAbility</p></td><td class="cellrowborder" valign="top" width="10.000000000000002%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p16091064576">MaximizeFullScreenEntryAbility</p></td></tr></tbody></table>

1.  创建WindowFullScreenHideAvoidAreaPage.ets用来承载十种沉浸式方案的入口，新建Item类用来承载需要跳转的EntryAbility的相关信息，并在aboutToAppear方法中实例化各个EntryAbility对应的Item，再通过startAbility方法跳转到对应方案的EntryAbility，示例代码如下：
    
    import { common, Want } from '@kit.AbilityKit';
    import { window } from '@kit.ArkUI';
    
    class Item {
      name: string = '';
      abilityName: string = '';
    
      constructor(name: string, abilityName: string) {
        this.name = name;
        this.abilityName = abilityName;
      }
    }
    
    @Entry
    @Component
    struct WindowFullScreenHideAvoidAreaPage {
      @State dataSource: Item\[\] = \[\];
      @State statusBarHeight: number = 0;
    
      aboutToAppear(): void {
        this.dataSource.push(new Item('沉浸式模式下显示避让区', 'ShowAvoidAreaEntryAbility'));
        this.dataSource.push(new Item('页面中设置沉浸式并隐藏避让区', 'HideAvoidAreaEntryAbility'));
        this.dataSource.push(new Item('通过设置窗口背景色实现沉浸式', 'WindowBackGroundColorEntryAbility'));
        this.dataSource.push(new Item('通过expandSafeArea实现沉浸式', 'ExpandSafeAreaEntryAbility'));
        this.dataSource.push(new Item('滚动类组件实现沉浸式', 'ScrollableComponentEntryAbility'));
        this.dataSource.push(new Item('通过background实现页面级沉浸式', 'PageLevelFullScreenEntryAbility'));
        this.dataSource.push(new Item('通过ignoreLayoutSafeArea实现页面级沉浸式',
          'PageFullScreenByIgnoreLayoutSafeAreaEntryAbility'));
        this.dataSource.push(new Item('通过ignoreLayoutSafeArea实现沉浸式并避让挖孔区',
          'IgnoreLayoutSafeAreaAvoidCutoutEntryAbility'));
        this.dataSource.push(new Item('自由多窗实现沉浸式', 'FreeWindowFullScreenEntryAbility'));
        let windowStage = AppStorage.get('windowStage') as window.WindowStage;
        windowStage.getMainWindow().then((data) => {
          if (data == null) {
            console.error('Failed to obtain the main window. Cause: The data is empty');
            return;
          }
          this.statusBarHeight = data.getWindowAvoidArea(window.AvoidAreaType.TYPE\_SYSTEM).topRect.height;
        });
      }
    
      build() {
        Column({ space: 15 }) {
          Text('实现沉浸式效果')
            .padding({ left: 20, top: 20 })
            .fontSize(25)
            .fontWeight(800)
            .width('100%')
            .textAlign(TextAlign.Start);
          List({ space: 20, initialIndex: 0 }) {
            ForEach(this.dataSource, (item: Item) => {
              ListItem() {
                Text(item.name)
                  .padding({
                    left: 20,
                    top: 20,
                    bottom: 20,
                    right: 20
                  })
                  .fontWeight(800)
                  .width('100%')
                  .fontSize(20)
                  .textAlign(TextAlign.Start)
                  .borderRadius(10)
                  .backgroundColor(0xFFFFFF);
              }
              .onClick(() => {
                this.jumpAbilityMethod(item.abilityName);
              });
            }, (item: Item) => item.name);
          }
          .listDirection(Axis.Vertical) _// 排列方向_
          .scrollBar(BarState.Off)
          .friction(0.6)
          .edgeEffect(EdgeEffect.Spring) _// 边缘效果设置为Spring_
          .width('90%');
        }
        .padding({ top: this.getUIContext().px2vp(this.statusBarHeight), bottom: 100 })
        .backgroundColor('#f1f3f5')
        .justifyContent(FlexAlign.Center)
        .alignItems(HorizontalAlign.Center)
        .height('100%')
        .width('100%');
      }
    
      jumpAbilityMethod(abilityName: string) {
        let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
        let want: Want = {
          deviceId: '',
          bundleName: 'com.example.windowfullscreendemo',
          abilityName: abilityName
        };
        try {
          context.startAbility(want, (err: BusinessError) => {
            if (err.code) {
              _// 处理业务逻辑错误_
              console.error(\`startAbility failed, code is ${err.code}, message is ${err.message}\`);
              return;
            }
            _// 执行正常业务_
            console.info('startAbility succeed');
          });
        } catch (err) {
          _// 处理入参错误异常_
          let code = (err as BusinessError).code;
          let message = (err as BusinessError).message;
          console.error(\`startAbility failed, code is ${code}, message is ${message}\`);
        }
      }
    }
    
2.  在EntryAbility.ets的loadContent方法中加载WindowFullScreenHideAvoidAreaPage.ets页面，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class EntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/WindowFullScreenHideAvoidAreaPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
          let windowClass: window.Window = windowStage.getMainWindowSync(); _// 获取应用主窗口_
          _// 1. 设置窗口全屏_
          let isLayoutFullScreen = true;
          windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
            console.info('Succeeded in setting the window layout to full-screen mode.');
          }).catch((err: BusinessError) => {
            console.error(\`Failed to set the window layout to full-screen mode. Code is ${err.code}, message is ${err.message}\`);
          });
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/0c/v3/qENjfECMQv-uJfaUcmt9ZA/zh-cn_image_0000002628567252.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=BA5F7720D4B2B1DF665B8CFD20A84F82BDCF24BF4ACCB560C810623B83C8E3DA "点击放大")

#### \[h2\]方案一：沉浸式模式下显示避让区

通过窗口的setWindowLayoutFullScreen方法设置窗口为沉浸式，并用getWindowAvoidArea接口获取状态栏和导航条的高度，保存在AppStorage中，并在页面中获取ShowAvoidAreaEntryAbility.ets中保存的状态栏和导航条的高度，并通过padding给页面设置内边距以达到避让导航栏的效果，在本例中通过Toggle组件来控制是否避让状态栏和导航条。

1.  在ShowAvoidAreaEntryAbility.ets中通过窗口的setWindowLayoutFullScreen方法设置窗口为沉浸式，并用getWindowAvoidArea接口获取状态栏和导航条的高度，保存在AppStorage中，以供后续在页面中获取状态栏和导航条的高度，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class ShowAvoidAreaEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/ShowAvoidAreaPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          let windowClass: window.Window = windowStage.getMainWindowSync(); _// 获取应用主窗口_
          _// 1. 设置窗口全屏_
          let isLayoutFullScreen = true;
          windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
            console.info('Succeeded in setting the window layout to full-screen mode.');
          }).catch((err: BusinessError) => {
            console.error(\`Failed to set the window layout to full-screen mode. Code is ${err.code}, message is ${err.message}\`);
          });
          _// 获取导航条和状态栏高度_
          let type = window.AvoidAreaType.TYPE\_NAVIGATION\_INDICATOR; _// 此处以导航条避让为例_
          let avoidArea = windowClass.getWindowAvoidArea(type);
          let bottomRectHeight = avoidArea.bottomRect.height; _// 获取到导航区域的高度_
          AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);
          type = window.AvoidAreaType.TYPE\_SYSTEM; _// 以状态栏避让为例_
          avoidArea = windowClass.getWindowAvoidArea(type);
          let topRectHeight = avoidArea.topRect.height; _// 获取状态栏区域高度_
          AppStorage.setOrCreate('topRectHeight', topRectHeight);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
          windowClass.on('avoidAreaChange', (data) => {
            if (data.type === window.AvoidAreaType.TYPE\_SYSTEM) {
              let topRectHeight = data.area.topRect.height;
              AppStorage.setOrCreate('topRectHeight', topRectHeight);
            } else if (data.type == window.AvoidAreaType.TYPE\_NAVIGATION\_INDICATOR) {
              let bottomRectHeight = data.area.bottomRect.height;
              AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);
            }
          });
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  在ShowAvoidAreaPage.ets页面中用@StorageProp获取ShowAvoidAreaEntryAbility.ets中保存的状态栏和导航条的高度，并通过padding给页面设置内边距以达到避让导航栏的效果，在本例中通过Toggle组件来控制是否避让状态栏和导航条，示例代码如下：
    
    @Entry
    @Component
    struct ShowAvoidAreaPage {
      @StorageProp('bottomRectHeight') bottomRectHeight: number = 0;
      @StorageProp('topRectHeight') topRectHeight: number = 0;
      @State isAvoidStatus: boolean = false;
      @State isAvoidNavigationIndicator: boolean = false;
    
      build() {
        Column() {
          Column({ space: 35 }) {
            Row({ space: 30 }) {
              if (this.isAvoidStatus) {
                Text('避让状态栏:是');
              } else {
                Text('避让状态栏:否');
              }
              Toggle({ type: ToggleType.Switch, isOn: this.isAvoidStatus })
                .scale({ x: 2, y: 2 })
                .onChange((isOn: boolean) => {
                  this.isAvoidStatus = isOn;
                });
            };
    
            Row({ space: 30 }) {
              if (this.isAvoidNavigationIndicator) {
                Text('避让导航条:是');
              } else {
                Text('避让导航条:否');
              }
              Toggle({ type: ToggleType.Switch, })
                .scale({ x: 2, y: 2 })
                .onChange((isOn: boolean) => {
                  this.isAvoidNavigationIndicator = isOn;
                });
            };
          }
          .justifyContent(FlexAlign.Center)
          .alignItems(HorizontalAlign.Center)
          .backgroundColor('#f1f3f5')
          .height('100%')
          .width('100%');
        }
        .padding({
          top: this.isAvoidStatus ? this.getUIContext().px2vp(this.topRectHeight) : 0,
          bottom: this.isAvoidNavigationIndicator ? this.getUIContext().px2vp(this.bottomRectHeight) : 0
        })
        .height('100%')
        .width('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e9/v3/pSbgQ6XNRs6OaFnfSd7bdw/zh-cn_image_0000002658926565.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=86BED77EC3C74EBCDF780D688DBA7ECE1EF2E74F96501A8F6C7E619035FAE466 "点击放大")

#### \[h2\]方案二：页面中设置沉浸式并隐藏避让区

1.  在HideAvoidAreaEntryAbility.ets的loadContent方法中加载Index.ets页面，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class HideAvoidAreaEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/Index', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  在Index.ets页面中通过setWindowLayoutFullScreen方法设置窗口沉浸式，并通过setSpecificSystemBarEnabled方法配合Toggle组件控制状态栏和导航条的显示和隐藏，示例代码如下：
    
    import { window } from '@kit.ArkUI';
    
    @Entry
    @Component
    struct Index {
      @State isShowStatus: boolean = false;
      @State isShowNavigationIndicator: boolean = false;
      private windowStage = AppStorage.get('windowStage') as window.WindowStage;
      private windowClass: window.Window = this.windowStage.getMainWindowSync(); _// 获取应用主窗口_
    
      aboutToAppear(): void {
        _// 1. 设置窗口全屏_
        let isLayoutFullScreen = true;
        this.windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
          console.info('Succeeded in setting the window layout to full-screen mode.');
        }).catch((err: BusinessError) => {
          console.error(\`Failed to set the window layout to full-screen mode. Code is ${err.code}, message is ${err.message}\`);
        });
        _// 1. 设置状态栏隐藏_
        this.setStatusMethod();
        _// 2. 设置导航区域隐藏_
        this.setNavigationIndicatorMethod();
      }
    
      setStatusMethod() {
        this.windowClass.setSpecificSystemBarEnabled('status', this.isShowStatus).then(() => {
          console.info('Succeeded in setting the status bar to be invisible.');
        }).catch((err: BusinessError) => {
          console.error(\`Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}\`);
        });
      }
    
      setNavigationIndicatorMethod() {
        this.windowClass.setSpecificSystemBarEnabled('navigationIndicator', this.isShowNavigationIndicator).then(() => {
          console.info('Succeeded in setting the navigation indicator to be invisible.');
        }).catch((err: BusinessError) => {
          console.error(\`Failed to set the navigation indicator to be invisible. Code is ${err.code}, message is ${err.message}\`);
        });
      }
    
      build() {
        Stack() {
          Image($r('app.media.img\_2'))
            .height('100%')
            .width('100%');
          Column() {
            Column({ space: 35 }) {
              Row({ space: 30 }) {
                if (this.isShowStatus) {
                  Text('状态栏隐藏:否');
                } else {
                  Text('状态栏隐藏:是');
                }
                Toggle({ type: ToggleType.Switch, isOn: this.isShowStatus })
                  .scale({ x: 2, y: 2 })
                  .onChange((isOn: boolean) => {
                    this.isShowStatus = isOn;
                    this.setStatusMethod();
                  });
              };
    
              Row({ space: 30 }) {
                if (this.isShowNavigationIndicator) {
                  Text('导航条隐藏:否');
                } else {
                  Text('导航条隐藏:是');
                }
                Toggle({ type: ToggleType.Switch, isOn: this.isShowNavigationIndicator })
                  .scale({ x: 2, y: 2 })
                  .onChange((isOn: boolean) => {
                    this.isShowNavigationIndicator = isOn;
                    this.setNavigationIndicatorMethod();
                  });
              };
            };
    
          };
        }
        .backgroundColor('#f1f3f5')
        .height('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/b2/v3/0wxBt721S8-TgLlYcZmLvA/zh-cn_image_0000002658806621.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=420472EBFE462ABBA19140549B81BA0B681D8D34FCA6B50695B16F9224288855 "点击放大")

#### \[h2\]方案三：通过设置窗口背景色实现沉浸式

1.  在WindowBackGroundColorEntryAbility.ets文件中的loadContent方法中加载下一步创建的WindowBackGroundColorDemo.ets页面，并保存windowStage到AppStorage中，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class WindowBackGroundColorEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/WindowBackGroundColorDemo', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  创建WindowBackGroundColorDemo.ets页面，并通过setWindowBackgroundColor和Toggle组件配合来控制是否设置窗口背景色，示例代码如下：
    
    import { window } from '@kit.ArkUI';
    
    @Entry
    @Component
    struct WindowBackGroundColorDemo {
      private windowStage = AppStorage.get('windowStage') as window.WindowStage;
      private windowClass: window.Window = this.windowStage.getMainWindowSync(); _// 获取应用主窗口_
      @State isWindowBackGroundColor: boolean = false;
    
      build() {
        RelativeContainer() {
          Row({ space: 30 }) {
            if (this.isWindowBackGroundColor) {
              Text('设置窗口背景色：是')
                .fontSize(13)
                .fontWeight(FontWeight.Bold);
            } else {
              Text('设置窗口背景色：否')
                .fontSize(13)
                .fontWeight(FontWeight.Bold);
            }
    
            Toggle({ type: ToggleType.Switch })
              .scale({ x: 2, y: 2 })
              .onChange((isOn: boolean) => {
                this.isWindowBackGroundColor = isOn;
                if (isOn) {
                  this.windowClass.setWindowBackgroundColor('#f1f3f5');
                } else {
                  this.windowClass.setWindowBackgroundColor('#ffffff');
                }
              });
          }
          .alignRules({
            center: { anchor: '\_\_container\_\_', align: VerticalAlign.Center },
            middle: { anchor: '\_\_container\_\_', align: HorizontalAlign.Center }
          });
        }
        .backgroundColor('#f1f3f5')
        .height('100%')
        .width('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/44/v3/hsTXmEb1Tj6DQQ-fx-DHIA/zh-cn_image_0000002628407356.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=33A33A3BB2A13892322A615693A7F4201EC4F88E2663AF0C105B3BAFD7FD58A4 "点击放大")

#### \[h2\]方案四：通过expandSafeArea实现沉浸式

1.  创建ExpandSafeAreaPage.ets页面，并用expandSafeArea属性配合Toggle组件控制组件扩展其安全区域，示例代码如下：
    
    @Entry
    @Component
    struct ExpandSafeAreaPage {
      @State topSafeAreaArr: Array<SafeAreaEdge> = \[\];
      @State bottomSafeAreaArr: Array<SafeAreaEdge> = \[\];
    
      build() {
        Column() {
          Row() {
            Text('顶部内容区域').fontSize(40).textAlign(TextAlign.Center).width('100%');
          }.backgroundColor('#d1d1d6')
          _// 设置顶部绘制延伸到状态栏_
          .expandSafeArea(\[SafeAreaType.SYSTEM\], this.topSafeAreaArr);
    
          Column({ space: 30 }) {
            Row({ space: 30 }) {
              if (this.topSafeAreaArr.length > 0) {
                Text('避让状态栏：否');
              } else {
                Text('避让状态栏：是');
              }
              Toggle({ type: ToggleType.Switch })
                .scale({ x: 2, y: 2 })
                .onChange((isOn: boolean) => {
                  if (isOn) {
                    this.topSafeAreaArr = \[SafeAreaEdge.TOP\];
                  } else {
                    this.topSafeAreaArr = \[\];
                  }
                });
            };
    
            Row({ space: 30 }) {
              if (this.bottomSafeAreaArr.length > 0) {
                Text('避让导航条：否');
              } else {
                Text('避让导航条：是');
              }
              Toggle({ type: ToggleType.Switch })
                .scale({ x: 2, y: 2 })
                .onChange((isOn: boolean) => {
                  if (isOn) {
                    this.bottomSafeAreaArr = \[SafeAreaEdge.BOTTOM\];
                  } else {
                    this.bottomSafeAreaArr = \[\];
                  }
                });
            };
          };
    
          Row() {
            Text('底部内容区域').fontSize(40).textAlign(TextAlign.Center).width('100%');
          }.backgroundColor('#d1d1d6')
          _// 设置底部绘制延伸到导航区域_
          .expandSafeArea(\[SafeAreaType.SYSTEM\], this.bottomSafeAreaArr);
        }
        .width('100%')
        .height('100%')
        .alignItems(HorizontalAlign.Center)
        .backgroundColor('#f1f3f5')
        .justifyContent(FlexAlign.SpaceBetween);
      }
    }
    
2.  在ExpandSafeAreaEntryAbility.ets文件中的loadContent方法中加载ExpandSafeAreaPage.ets页面，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class ExpandSafeAreaEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/ExpandSafeAreaPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/91/v3/Tfm5zK6TQhqTEFkOCQQWrg/zh-cn_image_0000002628567254.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=F248CA7E1B2A0EB72F48C8E3466715A6B40E265A2E8A3F5271C9205431E95C7C "点击放大")

#### \[h2\]方案五：滚动类组件实现沉浸式

1.  在ScrollableComponentEntryAbility.ets文件中的loadContent方法中加载下一步创建的ScrollableComponentPage.ets页面，并通过窗口的setWindowBackgroundColor方法设置背景色，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class ScrollableComponentEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/ScrollableComponentPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          windowStage.getMainWindowSync().setWindowBackgroundColor('#f1f3f5'); _// 配置窗口整体底色_
        });
      }
    
      onWindowStageDestroy(): void {
       _ // Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  创建ScrollableComponentPage.ets页面，使用expandSafeArea属性配合Toggle组件来控制List组件是否扩展到状态栏或者导航条，示例代码如下：
    
    _// xxx.ets_
    @Entry
    @Component
    struct ScrollableComponentPage {
      private dataSource: number\[\] = \[0, 1, 2, 3, 4, 5, 6, 7, 8, 9\];
      @State arr: Array<SafeAreaEdge> = \[\];
      @State safeAreaTypeArr: Array<SafeAreaType> = \[\];
    
      build() {
        Column() {
          List({ space: 20, initialIndex: 0 }) {
            ForEach(this.dataSource, (item: number, index: number) => {
              ListItem() {
                Column({ space: 30 }) {
                  Text(item.toString())
                    .fontSize(20)
                    .textAlign(TextAlign.Center)
                    .width('100%');
                  if (index == 5) {
                    Row({ space: 30 }) {
                      if (this.arr.includes(SafeAreaEdge.TOP)) {
                        Text('避让状态栏：否');
                      } else {
                        Text('避让状态栏：是');
                      }
                      Toggle({ type: ToggleType.Switch })
                        .onChange((isOn: boolean) => {
                          if (isOn) {
                            if (!this.safeAreaTypeArr.includes(SafeAreaType.SYSTEM)) {
                              this.safeAreaTypeArr.push(SafeAreaType.SYSTEM);
                            }
                            this.arr.push(SafeAreaEdge.TOP);
                          } else {
                            if (!this.arr.includes(SafeAreaEdge.BOTTOM)) {
                              this.safeAreaTypeArr = \[\];
                            }
                            this.arr = this.arr.filter(item => item == SafeAreaEdge.TOP);
                          }
                        });
                    };
    
                    Row({ space: 30 }) {
                      if (this.arr.includes(SafeAreaEdge.BOTTOM)) {
                        Text('避让导航条：否');
                      } else {
                        Text('避让导航条：是');
                      }
                      Toggle({ type: ToggleType.Switch })
                        .onChange((isOn: boolean) => {
                          if (isOn) {
                            if (!this.safeAreaTypeArr.includes(SafeAreaType.SYSTEM)) {
                              this.safeAreaTypeArr.push(SafeAreaType.SYSTEM);
                            }
                            this.arr.push(SafeAreaEdge.BOTTOM);
                          } else {
                            this.arr = this.arr.filter(item => item == SafeAreaEdge.BOTTOM);
                          }
                        });
                    };
                  }
                }
                .padding({ top: 20, bottom: 20 })
                .borderRadius(10)
                .backgroundColor(0xFFFFFF);
              };
            }, (item: number) => item.toString());
          }
          .listDirection(Axis.Vertical) _// 排列方向_
          .scrollBar(BarState.Off)
          .friction(0.6)
          .edgeEffect(EdgeEffect.Spring) _// 边缘效果设置为Spring_
          .width('90%')
          _// List组件的视窗范围扩展至导航条或者状态栏。_
          .expandSafeArea(this.safeAreaTypeArr, this.arr);
        }
        .width('100%')
        .height('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/82/v3/442sXtCJRnKPqrBj0Sf3Sw/zh-cn_image_0000002658926567.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=3BD6BD1339A9287269C72DF373E8B7111CC05D5696F8D6036D3C3300F3B41C01 "点击放大")

#### \[h2\]方案六：通过background实现页面级沉浸式

1.  创建PageLevelFullScreenByBackgroundPage.ets页面，通过background设置页面的背景来实现沉浸式效果，示例代码如下：
    
    @Entry
    @Component
    struct PageLevelFullScreenByBackgroundPage {
      private message: string =
        '从1978年实行改革开放,到2001年加入世贸组织,再到党的十八大以来更大力度实施对外开放,' +
          '我国的开放大门越开越大,经济也越来越强。' +
          '通过不断开放市场,扩大准入,引入外资企业参与国内市场竞争从1978年实行改革开放。';
    
      build() {
        RelativeContainer() {
          Text(this.message)
            .padding({ left: 12, right: 15 })
            .fontSize(18)
            .fontWeight(FontWeight.Bold)
            .alignRules({
              center: { anchor: '\_\_container\_\_', align: VerticalAlign.Center },
              middle: { anchor: '\_\_container\_\_', align: HorizontalAlign.Center }
            });
        }
        _// 通过background属性实现单页面的沉浸式_
        .background('#f1f3f5')
        .height('100%')
        .width('100%');
      }
    }
    
2.  在PageLevelFullScreenEntryAbility.ets文件中的loadContent方法中加载PageLevelFullScreenByBackgroundPage.ets页面，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class PageLevelFullScreenEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/PageLevelFullScreenByBackgroundPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/cf/v3/k5HweU4KQUa0NY2V8Pqwhw/zh-cn_image_0000002658806623.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=E852D4CF255E34450F10519D744170BA33D49093A53E0B3E2501460CADD2022C "点击放大")

#### \[h2\]方案七：通过ignoreLayoutSafeArea实现页面级沉浸式

1.  在PageFullScreenByIgnoreLayoutSafeAreaEntryAbility.ets文件中的loadContent方法中加载下一步创建的PageFullScreenByIgnoreLayoutSafeAreaPage.ets页面，并通过AppStorage保存windowStage，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class PageFullScreenByIgnoreLayoutSafeAreaEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/PageFullScreenByIgnoreLayoutSafeAreaPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
       _ // Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  创建PageFullScreenByIgnoreLayoutSafeAreaPage.ets页面，并在aboutToAppear生命周期中通过getMainWindow方法获取当前主窗口实例，再通过getWindowAvoidArea获取当前状态栏和导航条的高度，并设置ignoreLayoutSafeArea和组件的高度为LayoutPolicy.matchParent，再通过padding属性设置组件的内边距达到避让的效果，示例代码如下：
    
    import window from '@ohos.window';
    
    @Entry
    @Component
    struct PageFullScreenByIgnoreLayoutSafeAreaPage {
      @State statusBarHeight: number = 0;
      @State navigationIndicatorHeight: number = 0;
      private dataSource: number\[\] = \[\];
    
      aboutToAppear(): void {
        for (let index = 0; index < 30; index++) {
          this.dataSource.push(index);
        }
        let windowStage = AppStorage.get('windowStage') as window.WindowStage;
        windowStage.getMainWindow().then((data) => {
          if (data == null) {
            console.error('Failed to obtain the main window. Cause: The data is empty');
            return;
          }
          this.statusBarHeight = data.getWindowAvoidArea(window.AvoidAreaType.TYPE\_SYSTEM).topRect.height;
          this.navigationIndicatorHeight =
            data.getWindowAvoidArea(window.AvoidAreaType.TYPE\_NAVIGATION\_INDICATOR).bottomRect.height;
        }).catch((err: BusinessError) => {
          console.error(\`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}\`);
        });
      }
    
      build() {
        Column() {
          Column() {
            List({ space: 20, initialIndex: 0 }) {
              ForEach(this.dataSource, (item: number) => {
                ListItem() {
                  Text('' + item)
                    .width('100%')
                    .height(100)
                    .fontSize(16)
                    .textAlign(TextAlign.Center)
                    .borderRadius(10)
                    .backgroundColor('#d1d1d6');
                };
              }, (item: number) => item.toString());
            }
            .listDirection(Axis.Vertical) _// 排列方向_
            .scrollBar(BarState.Off)
            .width('90%');
          }
          .width('100%')
          .ignoreLayoutSafeArea()
          .height(LayoutPolicy.matchParent)
          .backgroundColor('#f1f3f5')
          .padding({
            top: this.getUIContext().px2vp(this.statusBarHeight),
            bottom: this.getUIContext().px2vp(this.navigationIndicatorHeight)
          });
        }
        .width('100%')
        .height('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/6f/v3/it_-gmO2TLmNuIzkbFBocA/zh-cn_image_0000002628407360.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=B8CF3D5D1720B4C1CB6740B652D26A506F23A06DCD925D0C10EBCDA88917859F "点击放大")

#### \[h2\]方案八：通过ignoreLayoutSafeArea实现沉浸式并避让挖孔区

1.  在IgnoreLayoutSafeAreaAvoidCutoutEntryAbility.ets文件中的loadContent方法中加载下一步创建的IgnoreLayoutSafeAreaAvoidCutoutPage.ets页面，并通过AppStorage保存windowStage，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class IgnoreLayoutSafeAreaAvoidCutoutEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/IgnoreLayoutSafeAreaAvoidCutoutPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  在IgnoreLayoutSafeAreaAvoidCutoutPage.ets文件中，首次进入页面通过getWindowAvoidArea方法获取挖孔区的高度和距离屏幕顶部的高度，再通过setSpecificSystemBarEnabled方法设置状态栏和导航栏隐藏，最后通过on('avoidAreaChange')方法监听挖孔区的方向，根据此方向，通过padding设置组件的内边距，达到避让挖孔区的效果，示例代码如下：
    
    import { display, window } from '@kit.ArkUI';
    
    @Entry
    @Component
    struct IgnoreLayoutSafeAreaAvoidCutoutPage {
      @State avoidAreaPadding: Padding = {};
      private windowStage = AppStorage.get('windowStage') as window.WindowStage;
      private windowClass: window.Window = this.windowStage.getMainWindowSync(); _// 获取应用主窗口_
    
      _// 避让挖孔区_
      aboutToAppear(): void {
        this.avoidAreaPadding.top =
          this.getUIContext()
            .px2vp(this.windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE\_CUTOUT).topRect.height +
            this.windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE\_CUTOUT).topRect.top) + 10;
        this.windowClass.setSpecificSystemBarEnabled('status', false).then(() => {
          console.info('Succeeded in setting the status bar to be invisible.');
        }).catch((err: BusinessError) => {
          console.error(\`Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}\`);
        });
        this.windowClass.setSpecificSystemBarEnabled('navigationIndicator', false).then(() => {
          console.info('Succeeded in setting the navigation indicator to be invisible.');
        }).catch((err: BusinessError) => {
          console.error(\`Failed to set the navigation indicator to be invisible. Code is ${err.code}, message is ${err.message}\`);
        });
        try {
          this.windowClass.on('avoidAreaChange', (data) => {
            if (data.type === window.AvoidAreaType.TYPE\_CUTOUT) {
              this.updateAvoidPadding(data.area);
            }
          });
        } catch (exception) {
          console.error(\`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}\`);
        }
      }
    
      updateAvoidPadding(area: window.AvoidArea) {
        try {
          _// Top cutout area: set top padding._
          if (area.topRect.height > 0) {
            this.avoidAreaPadding.top = this.getUIContext().px2vp(area.topRect.height + area.topRect.top) + 10;
            this.avoidAreaPadding.left = 0;
            this.avoidAreaPadding.right = 0;
            this.avoidAreaPadding.bottom = 0;
          }
          _// Left cutout area: set left padding._
          if (area.leftRect.width > 0) {
            this.avoidAreaPadding.left = this.getUIContext().px2vp(area.leftRect.left + area.leftRect.width) + 10;
            this.avoidAreaPadding.top = 0;
            this.avoidAreaPadding.right = 0;
            this.avoidAreaPadding.bottom = 0;
          }
          _// Right cutout area: set right padding._
          if (area.rightRect.width > 0) {
            this.avoidAreaPadding.right =
              this.getUIContext().px2vp(display.getDefaultDisplaySync().width - area.rightRect.left) + 10;
            this.avoidAreaPadding.top = 0;
            this.avoidAreaPadding.left = 0;
            this.avoidAreaPadding.bottom = 0;
          }
          _// Bottom cutout area: set bottom padding._
          if (area.bottomRect.height > 0) {
            this.avoidAreaPadding.bottom =
              this.getUIContext().px2vp(display.getDefaultDisplaySync().height - area.bottomRect.top) + 10;
            this.avoidAreaPadding.top = 0;
            this.avoidAreaPadding.right = 0;
            this.avoidAreaPadding.left = 0;
          }
        } catch (error) {
          let err = error as BusinessError;
          console.info('TestLog', \`Failed to set avoid padding. Code: ${err.code}, message: ${err.message}\`);
        }
      }
    
      build() {
        Column() {
          Image($r('app.media.img'));
        }
        .padding(this.avoidAreaPadding)
        .height('100%')
        .width('100%');
      }
    }
    

实现效果如下：

<table id="zh-cn_topic_0000002530313716_table943391919357"><tbody><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row5433151916351"><th class="firstcol" id="mcps1.3.11.4.1.3.1.1" valign="top" width="30%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p243371911356">正向竖屏</p></th><td class="cellrowborder" valign="top" width="70%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p16433719153517"><span><img height="549.7821" originheight="835" originwidth="404" src="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c4/v3/iOB4lnyIRPakh8DjOjx5-Q/zh-cn_image_0000002628567256.png?HW-CC-KV=V1&amp;HW-CC-Date=20260709T134012Z&amp;HW-CC-Expire=86400&amp;HW-CC-Sign=5C20D871E039D33174314FE501D61D9680A34B4DFFBE197DDA4FA9FD07BC802B" title="点击放大" width="266"></span></p></td></tr><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row12433619113514"><th class="firstcol" id="mcps1.3.11.4.1.3.2.1" valign="top" width="30%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p3433119173514">横屏</p></th><td class="cellrowborder" valign="top" width="70%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p143331913357"><span><img height="266" originheight="432" originwidth="894" src="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e8/v3/6mbm5UZWToqd6WUDoe1UFw/zh-cn_image_0000002658926569.png?HW-CC-KV=V1&amp;HW-CC-Date=20260709T134012Z&amp;HW-CC-Expire=86400&amp;HW-CC-Sign=A5780385F5EB78980D1238D0841856EB46CFFB6C02CECA8023C73D18AB4EE6AF" title="点击放大" width="550.4737"></span></p></td></tr><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row14433121943510"><th class="firstcol" id="mcps1.3.11.4.1.3.3.1" valign="top" width="30%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p3433201918351">反向竖屏</p></th><td class="cellrowborder" valign="top" width="70%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p14331619163515"><span><img height="549.822" originheight="833" originwidth="403" src="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/e4/v3/SG3-0sGoT6ukvg4MlV2wMg/zh-cn_image_0000002658806627.png?HW-CC-KV=V1&amp;HW-CC-Date=20260709T134012Z&amp;HW-CC-Expire=86400&amp;HW-CC-Sign=A4C3075873ED4753E794CADEBEC7CC338F1B00F7D3E8154DD6202D1190F111A8" title="点击放大" width="266"></span></p></td></tr><tr id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_row1843351993518"><th class="firstcol" id="mcps1.3.11.4.1.3.4.1" valign="top" width="30%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p1443381993517">反向横屏</p></th><td class="cellrowborder" valign="top" width="70%"><p id="ZH-CN_TOPIC_0000002624941110__zh-cn_topic_0000002530313716_p1243317198358"><span><img height="266" originheight="397" originwidth="820" src="https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/35/v3/jCkdxjyNSRaItHbXgzSW3g/zh-cn_image_0000002628407362.png?HW-CC-KV=V1&amp;HW-CC-Date=20260709T134012Z&amp;HW-CC-Expire=86400&amp;HW-CC-Sign=270F58A9EF60269EA1DE88D404A0895E7631165DD4448AE3F8FCE0D70259F643" title="点击放大" width="549.4230000000001"></span></p></td></tr></tbody></table>

#### \[h2\]方案九：自由多窗实现全屏和非全屏模式时的沉浸式

1.  在FreeWindowFullScreenEntryAbility.ets文件中的loadContent方法中加载下一步创建的FreeWindowFullScreenPage.ets页面，并通过AppStorage保存windowStage，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class FreeWindowFullScreenEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        windowStage.loadContent('pages/FreeWindowFullScreenPage', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          AppStorage.setOrCreate('windowStage', windowStage);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  创建FreeWindowFullScreenPage.ets页面，通过setWindowDecorVisible(false)接口设置标题栏的隐藏，仅保留右上角的三键区，setWindowDecorHeight()接口设置标题栏高度，控制右上角三键区显示高度，on('windowTitleButtonRectChange')注册右上角三键大小变化监听，用于页面布局避让,示例代码如下：
    
    import { window } from '@kit.ArkUI';
    
    @Entry
    @Component
    struct FreeWindowFullScreenPage {
      @State itleButtonWidth: number = 0;
    
      aboutToAppear(): void {
        let windowStage = AppStorage.get('windowStage') as window.WindowStage;
        let windowClass: window.Window = windowStage.getMainWindowSync(); _// 获取应用主窗口_
        windowClass.setWindowDecorHeight(56);
        windowClass.setWindowDecorVisible(false);
        try {
          windowClass.on('windowTitleButtonRectChange', (titleButtonRect) => {
            this.itleButtonWidth = titleButtonRect.width;
          });
        } catch (exception) {
          console.error(\`Failed to enable the listener for window title buttons area changes. Cause code: ${exception.code}, message: ${exception.message}\`);
        }
      }
    
      build() {
        Column() {
          Text('自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例')
            .height(30)
            .padding({ left: 12, top: 10, right: this.itleButtonWidth });
        }
        .backgroundColor('#f1f3f5')
        .height('100%')
        .width('100%');
      }
    }
    

实现效果如下：

| 
全屏

 | 

非全屏

 |
| :-- | :-- |
| 

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/96/v3/c3qtwDEyQJC6Z4lsXy5LZw/zh-cn_image_0000002628567258.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=3C59F5D42133A2DD025D410EDB40A5B10CDFECD96051BE446FD66A2B27D6BCD7 "点击放大")

 | 

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/9/v3/P6ynt7JuT8ifXTlbNMD6eA/zh-cn_image_0000002658926571.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=34B5C8D8E637890800E64AB24E44738E8CDFE368E5F31B92DC690D7186E8F837 "点击放大")

 |

#### \[h2\]方案十：自由多窗实现仅在全屏模式时的沉浸式

1.  在MaximizeFullScreenEntryAbility.ets文件中通过getMainWindowSync接口获取当前应用的主窗口，再通过maximize接口设置应用为全屏模式，通过setTitleAndDockHoverShown接口隐藏主窗口全屏模式下的标题栏和Dock栏，示例代码如下：
    
    import { UIAbility } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    
    const DOMAIN = 0x0000;
    
    export default class MaxmizeFullScreenEntryAbility extends UIAbility {
      onDestroy(): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        _// Main window is created, set main page for this ability_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    
        windowStage.loadContent('pages/MaximizeFullScreen', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          let windowClass: window.Window = windowStage.getMainWindowSync(); _// 获取应用主窗口_
          windowClass.maximize(window.MaximizePresentation.ENTER\_IMMERSIVE);
          windowClass.setTitleAndDockHoverShown(true, false);
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      onWindowStageDestroy(): void {
        _// Main window is destroyed, release UI related resources_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
      }
    
      onForeground(): void {
        _// Ability has brought to foreground_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
      }
    
      onBackground(): void {
        _// Ability has back to background_
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
      }
    };
    
2.  创建FreeWindowFullScreenPage.ets页面用以展示沉浸式效果，示例代码如下：
    
    @Entry
    @Component
    struct MaximizeFullScreen {
      build() {
        Column() {
          Text('自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例自由窗口标题栏沉浸案例')
            .height(30)
            .padding({ left: 30, top: 10 });
        }
        .backgroundColor('#f1f3f5')
        .height('100%')
        .width('100%');
      }
    }
    

实现效果如下：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/1d/v3/vk1AS7wLRQS979ID61c3Ew/zh-cn_image_0000002658806629.png?HW-CC-KV=V1&HW-CC-Date=20260709T134012Z&HW-CC-Expire=86400&HW-CC-Sign=C91C2C3A1F2537A17F92C53FF1C62E6342BDC605A66D59367FADBCA9C8471F13 "点击放大")

#### 总结

一般来说，整个应用（所有页面）都需要沉浸式效果，可以选择设置窗口全屏方案统一实现，针对具体页面的避让场景设置padding；单个页面或者仅需要将背景延伸到状态栏和导航栏，页面内容（子组件）希望避让状态栏和导航栏，使用expandSafeArea属性扩展对应组件安全区域来实现沉浸式效果更为方便。