在HarmonyOS NEXT开发中深色模式后状态栏看不到状态信息(时间、电量等)?

在HarmonyOS NEXT开发中深色模式后状态栏看不到状态信息(时间、电量等)?

阅读 1.4k
avatarAI BotBETA

在HarmonyOS NEXT开发中遇到深色模式下状态栏信息不可见的问题,可通过以下方案解决:

  1. 状态栏颜色适配

    // 设置状态栏颜色和文字颜色
    window.setStatusBarColor("#000000", (err, data) => { 
     if (!err) {
         window.setStatusBarContentColor("#FFFFFF"); // 强制设置为白色
     }
    });
  2. 主题配置文件
    resources/base/theme/theme.json中添加:

    "dark": {
     "color": {
         "status_bar_background": "#1A1A1A",
         "status_bar_content": "#FFFFFF"
     }
    }
  3. XML布局声明

    <ohos:config
     xmlns:ohos="http://schemas.huawei.com/hap"
     ohos:mode="dark">
     <ohos:statusbar
         ohos:background="#1A1A1A"
         ohos:textColor="#FFFFFF"/>
    </ohos:config>

关键排查点

  • 检查是否同时启用了沉浸式状态栏模式
  • 验证API版本是否支持setStatusBarContentColor方法
  • 使用系统提供的语义化颜色常量:

    import color from '@ohos.color';
    window.setStatusBarContentColor(color.Color.White);

若仍存在问题,建议使用hdc shell dumpsys window policy命令查看当前状态栏的实际渲染参数。

2 个回答

具体解决方案如下:

import { AbilityConstant, Configuration, UIAbility, Want } from '@kit.AbilityKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
import { window } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
export default class EntryAbility extends UIAbility { 
  colorMode: number | undefined = 0 
 
  onConfigurationUpdate(newConfig: Configuration): void { 
    this.colorMode = newConfig.colorMode 
    let windowStage: window.WindowStage | undefined = AppStorage.get('windowStage') 
    if (windowStage !== undefined) { 
      this.changeColor(windowStage) 
    } 
  } 
 
 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
    this.colorMode = this.context.config.colorMode 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  changeColor(windowStage: window.WindowStage) { 
    let windowClass: window.Window | null 
    windowStage.getMainWindow((err: BusinessError, data) => { 
      let errCode: number = err.code; 
      if (errCode) { 
        console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 
        return; 
      } 
      windowClass = data; 
      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 
 
      let sysBarProps: window.SystemBarProperties = { 
        statusBarColor: this.colorMode === 0 ? '#aa00ff0b' : '#ff00ff', 
        navigationBarColor: '#00ff00', 
        // 以下两个属性从API Version 8开始支持 
        statusBarContentColor: this.colorMode === 0 ? '#aaff0000' : '#ff002aff', 
        navigationBarContentColor: '#ffffff' 
      }; 
      windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => { 
        let errCode: number = err.code; 
        if (errCode) { 
          console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); 
          return; 
        } 
        console.info('Succeeded in setting the system bar properties.'); 
      }); 
    }) 
  } 
 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    AppStorage.setOrCreate('windowStage', windowStage) 
 
    this.changeColor(windowStage) 
 
    windowStage.loadContent('pages/Index', (err) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); 
    }); 
 
 
  } 
 
  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'); 
  } 
}

页面状态栏的适配:

@State isDarkMode: boolean = false
@State barContentColor: string = ''
private context = getContext(this) as common.UIAbilityContext
// 状态栏适配黑夜模式
onPageShow(): void {

window.getLastWindow(getContext(this), (err, win) => {
  //判断是否是暗夜模式(因为有三种)
  if(this.context.config.colorMode==1){
    this.isDarkMode = false
  }
  if(this.context.config.colorMode==0){
    this.isDarkMode = true
  }
  //因为barContentColor只能取string类型的值,所以不能直接用resource资源来适配
  this.barContentColor = this.isDarkMode ? '#ffffff' : '#000000'

  let SystemBarProperties: window.SystemBarProperties = {
    statusBarContentColor: this.barContentColor
  };
})

}

具体案例可以参考链接:https://developer.huawei.com/consumer/cn/forum/topic/02081531...

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