HarmonyOS 每个页面状态栏背景颜色和字体颜色不一样,怎么动态设置每个页面的沉浸式状态栏

目前尝试在每个页面生命周期onPageShow和aboutToApear中调用以上两个方法,但是有些页面颜色还是没变过来,出现了问题,生命周期中无法正确改变系统状态栏颜色

import { window } from '@kit.ArkUI';
async settingStatusBarColor(color: string) {
  try {
    const win = await window.getLastWindow(WindowManager.context)
    win.setWindowSystemBarProperties({ statusBarColor: color }) //设置安全区字体颜色为白色
  } catch (error) {
    hilog.error(0x0000,WindowManager.TAG, "设置顶部状态栏颜色失败,error:" + JSON.stringify(error))
  }
}
//设置安全区字体颜色为白色
static async settingStatusBarLight() {
  try {
    const win = await window.getLastWindow(WindowManager.context)
    win.setWindowSystemBarProperties({ statusBarContentColor: '#FFFFFF' })
  } catch (error) {
    hilog.error(0x0000,WindowManager.TAG, "设置顶部状态栏浅色模式失败,error:" + JSON.stringify(error))
  }
}
HarmonyOS
8h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

参考代码:

修改了一下代码
参考:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { WindowManager } from '../WindowManager';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    hilog.info(0x0000, 'testTag', JSON.stringify(this.context), 'context-tag');

    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    windowStage.loadContent('enablement/enablement', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }

      let windowClass = windowStage.getMainWindowSync();
      AppStorage.setOrCreate('windowClass', windowClass);

      WindowManager.enableFullScreen()
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
      // let windowClass: window.Window = windowStage.getMainWindowSync(); //获取应用主窗口
      // windowClass.setWindowLayoutFullScreen(true)
      // let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; //以导航条避让为例
      // let avoidArea = windowClass.getWindowAvoidArea(type);
      // let bottomRectHeight = px2vp(avoidArea.bottomRect.height); //获取到导航条区域的高度
      // AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
      // let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
      // let statusBarHeight = px2vp(area.topRect.height) //状态栏高度
      // AppStorage.setOrCreate('statusBarHeight', statusBarHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
      // hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
      // windowClass.setWindowSystemBarEnable(['status', 'navigation'])
      // windowClass.setWindowSystemBarProperties({
      //   statusBarColor: "#FFFFFF",
      //   statusBarContentColor: '#FFFFFF'
      // })

    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}

//WindowManager.ets

import
{
  window
}
from
'@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { LogUtil } from '@pura/harmony-utils';


// 设置沉浸式工具类
export class WindowManager {
  private static readonly TAG: string = "WindowManager---"

  //开启沉浸式全屏模式
  static enableFullScreen() {
    let win: window.Window = AppStorage.get('windowClass')!
    win.setWindowLayoutFullScreen(true) //使用setWindowLayoutFullScreen设置true开启沉浸式模式
    const topArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM) //使用getWindowAvoidArea方法获取到安全区域的高度
    let topHeight = px2vp(topArea.topRect.height)
    AppStorage.setOrCreate('topHeight', topHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
    const bottomArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
    let bottomHeight = px2vp(bottomArea.bottomRect.height)
    AppStorage.setOrCreate('bottomHeight', bottomHeight) //将高度转成vp,存到AppStorage中方便其它页面获取高度
    LogUtil.error(WindowManager.TAG, `topHeight:${topHeight},,,bottomHeight:${bottomHeight}`)
  }

  //关闭沉浸式模式
  static disableFullScreen() {
    let win: window.Window = AppStorage.get('windowClass')!
    win.setWindowLayoutFullScreen(false) //使用setWindowLayoutFullScreen设置false关闭沉浸式模式
    AppStorage.setOrCreate('topHeight', 0) //将高度重置为零
  }

  static settingStatusBarLight() {
    let win: window.Window = AppStorage.get('windowClass')!
    win.setWindowSystemBarProperties({ statusBarContentColor: '#FFFFFF' }) //设置安全区字体颜色为白色

  }

  static settingStatusBarDark() {
    let win: window.Window = AppStorage.get('windowClass')!
    win.setWindowSystemBarProperties({ statusBarContentColor: '#000000' }) //设置安全区字体颜色为黑色
  }

  static async keepScreenOn(isKeepScreenOn: boolean) {
    let win: window.Window = AppStorage.get('windowClass')!
    let promise = win.setWindowKeepScreenOn(isKeepScreenOn)
    promise?.then(() => {
      LogUtil.error(WindowManager.TAG, `${isKeepScreenOn ? "打开" : "关闭"}屏幕常亮成功`)
    }).catch((error: BusinessError) => {
      LogUtil.error(WindowManager.TAG,
        `${isKeepScreenOn ? "打开" : "关闭"}屏幕常亮异常,error:${JSON.stringify(error)}`)
    });

  }

  private static async setWindowBrightness(brightness: number) {
    let win: window.Window = AppStorage.get('windowClass')!

    let promise = win.setWindowBrightness(brightness)
    promise?.then(() => {
      LogUtil.error(WindowManager.TAG, "设置屏幕亮度成功")
    }).catch((error: BusinessError) => {
      LogUtil.error(WindowManager.TAG, "设置屏幕亮度异常,error:" + JSON.stringify(error))
    });

  }

  static setWindowMaxBrightness() {
    WindowManager.setWindowBrightness(1)
  }

  static setWindowDefaultBrightness() {
    WindowManager.setWindowBrightness(-1)
  }
}
分享
微博
QQ
微信
回复
6h前
相关问题
鸿蒙怎么 修改状态栏字体颜色
12440浏览 • 1回复 待解决
如何设置状态栏导航颜色
2918浏览 • 1回复 待解决
如何设置沉浸状态栏
2569浏览 • 1回复 待解决
HarmonyOS 沉浸状态栏
105浏览 • 1回复 待解决
如何实现沉浸状态栏
524浏览 • 1回复 待解决
HarmonyOS 沉浸状态栏最佳实践
294浏览 • 1回复 待解决
设置状态栏颜色不起作用怎么回事?
2431浏览 • 1回复 待解决
状态栏设置颜色失效该怎么办啊?
1952浏览 • 1回复 待解决
HarmonyOS 修改状态栏颜色不生效
303浏览 • 1回复 待解决