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

阅读 767
avatarAI BotBETA
在HarmonyOS NEXT中,要实现每个页面状态栏背景颜色和字体颜色不一样,并动态设置每个页面的沉浸式状态栏,你可以按照以下步骤进行:

1. **配置主题**:
   在你的每个页面的资源文件中(通常是`config.json`),配置一个透明的状态栏,这样你可以在每个页面中自定义状态栏的颜色。

2. **动态设置状态栏颜色**:
   在每个页面的代码中,使用`WindowManager`的API来动态设置状态栏的背景颜色和字体颜色。

// 获取WindowManager服务
WindowManager.LayoutConfig layoutConfig = getWindow().getAttributes();

// 设置状态栏背景颜色
int statusBarColor = Color.parseColor("#FF0000"); // 替换为你需要的颜色
getWindow().setStatusBarColor(statusBarColor);

// 设置状态栏字体颜色(根据背景颜色决定是深色还是浅色)
boolean isDarkText = (statusBarColor >> 24 & 0xFF) / 255.0 < 0.5; // 判断背景色深浅
getWindow().setStatusBarDarkMode(isDarkText ? false : true); // 浅色背景用深色文字,深色背景用浅色文字


3. **确保页面布局适应状态栏**:
在你的页面布局中,确保顶部有足够的空间来适应状态栏,避免内容被状态栏遮挡。

通过以上步骤,你可以在HarmonyOS NEXT中为每个页面动态设置不同的沉浸式状态栏背景颜色和字体颜色。注意,具体实现可能会根据API版本和具体需求有所不同,建议参考最新的HarmonyOS开发文档进行调整。
1 个回答

请参考如下代码:

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) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进