文档开发者中心
我的
|

开发实例

App开发

HarmonyOS 5

开发指南

HarmonyOS行业解决方案

求沉浸光感tab嵌套web的沉浸式模式安全区扩展适配方法

已解决

如图,尝试做了一个demo,发现始终解决不了底下的黑块,有没有大佬讲解一下怎么操作感谢🙏🙏

点赞
1
收藏
2
回复
5
分享
举报
浏览153 编辑于2026-04-20 03:08上海
全部评论
最多点赞
最新发布
最早发布
|
采纳答复

这个问题课可以查看窗口沉浸,我建议使用setWindowLayoutFullScreen()来实现应用全屏,并且弃用ignoreLayoutSafeArea()和expandSafeArea,这两个使用起来再多层组件嵌套的时候很容易混乱,不容易理清。可以查看以下代码:

收起
自动换行
深色代码主题
复制
onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    Logger.info(TAG, 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        Logger.error(TAG, `Failed to load the content. Cause: ${JSON.stringify(err) ?? ''}`);
        return;
      }
      Logger.info(TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data) ?? ''}`);
    });

    try {
      windowStage.getMainWindow((err, data) => {
        if (err.code) {
          Logger.error(TAG, `getMainWindow failed, code=${err.code}, msg=${err.message}`);
          return;
        }
        data.setWindowLayoutFullScreen(true).then(() => {
          Logger.info(TAG, `Succeeded to change full-screen state: ${true}.`);
        }).catch((err: Error) => {
          Logger.error(TAG, `Failed to change full-screen state. Cause message: ${JSON.stringify(err)}`);
        });
        data.setWindowBackgroundColor(Constants.BACKGROUND_COLOR)
      })
    } catch (error) {
      Logger.error(TAG, `Ability onWindowStageCreate, error=${JSON.stringify(error)}`);
    }
  }

应用设置全屏后,所有界面都会延伸到状态栏和导航栏,使窗口沉浸,如果想规避的话可以手动在顶部和底部添加空白组件,设置topRect和bottomRect,获取逻辑如下:

收起
自动换行
深色代码主题
复制
  /**
   * 初始化窗口属性
   * 获取窗口的初始尺寸、安全区信息等
   * 注意:断点由媒体查询系统统一管理,此处不设置断点
   * @param windowClass 窗口实例
   */
  private initializeWindowProperties(): void {
    const windowClass = this.mainWindowClass
    try {
      // 获取系统状态栏避让区域
      const systemAvoidArea = windowClass!.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
      this.windowProperties.topRectPx = systemAvoidArea.topRect.height
      this.windowProperties.topRect = this.uiContext!.px2vp(systemAvoidArea.topRect.height);
      Logger.info(TAG, `顶部安全区高度: ${this.windowProperties.topRectPx}px`);
      Logger.info(TAG, `顶部安全区高度: ${this.windowProperties.topRect}vp`);

      // 获取导航栏避让区域
      const navAvoidArea = windowClass!.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
      this.windowProperties.bottomRectPx = navAvoidArea.bottomRect.height
      this.windowProperties.bottomRect = this.uiContext!.px2vp(navAvoidArea.bottomRect.height);
      Logger.info(TAG, `底部安全区高度: ${this.windowProperties.bottomRectPx}px`);
      Logger.info(TAG, `底部安全区高度: ${this.windowProperties.bottomRect}vp`);

      // 获取窗口尺寸
      const windowSize = windowClass!.getWindowProperties().windowRect;
      this.windowProperties.winWidthPx = windowSize.width
      this.windowProperties.winHeightPx = windowSize.height
      this.windowProperties.winWidth = this.uiContext!.px2vp(windowSize.width);
      this.windowProperties.winHeight = this.uiContext!.px2vp(windowSize.height);
      Logger.info(TAG, `窗口尺寸: ${this.windowProperties.winWidth}x${this.windowProperties.winHeight}vp`);
      Logger.info(TAG, `窗口尺寸: ${this.windowProperties.winWidthPx}x${this.windowProperties.winHeightPx}px`);

      // 获取窗口装饰高度(如果支持)
      if (canIUse('SystemCapability.Window.SessionManager')) {
        const height = windowClass!.getWindowDecorHeight();
        this.windowProperties.decorHeightPx = height
        this.windowProperties.decorHeight = this.uiContext!.px2vp(height);
        Logger.info(TAG, `窗口装饰高度: ${this.windowProperties.decorHeight}vp`);
        Logger.info(TAG, `窗口装饰高度: ${this.windowProperties.decorHeightPx}px`);
      }
    } catch (error) {
      Logger.error(TAG, `初始化窗口属性失败: ${error.message}`);
    }
  }

获取避让高度后可以在UI中使用AppStorage使用。

展开全部
2楼回复于2026-04-17 08:54 来自北京

黑块是因为你系统是深色模式,调成浅色模式,底部就是白色遮罩了

其实应该是可以用主题色来控制的,但是要看你的代码支不支持

WithTheme({ colorMode: ThemeColorMode.LIGHT })

cke_3959.jpeg

3楼回复于2026-04-17 09:03 来自广东

在你的UIAbility中的生命周期onWindowStageCreate中设置应用浸式的效果就可以啦,

收起
自动换行
深色代码主题
复制
  onWindowStageCreate(windowStage: window.WindowStage): void {
    // 设置应用全屏沉浸式
    windowStage.getMainWindow((err: BusinessError, data: window.Window) => {
      data.setWindowLayoutFullScreen(true)
    })
    windowStage.loadContent('pages/Index', (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.');
    });
  }
5楼回复于2026-04-20 03:48 来自江苏
学习了
4楼回复于2026-04-17 09:32 来自浙江
1楼回复于2026-04-17 01:52 来自广东
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。