HarmonyOS 鸿蒙Next状态栏深色适配

HarmonyOS 鸿蒙Next状态栏深色适配

我按照官网的状态栏适配 为什么导入这个函数会报错

cke_148.png

报错信息是:Property ‘onConfigurationUpdate’ in type ‘EntryAbility’ is not assignable to the same property in base type ‘UIAbility’. Type ‘(newConfig: Configuration) => void’ is not assignable to type ‘(newConfig: import(“c:/Users/EDY/Desktop/xgl-pro/software/DevEco Studio/sdk/default/openharmony/ets/api/@ohos.app.ability.Configuration”).Configuration) => void’. Types of parameters ‘newConfig’ and ‘newConfig’ are incompatible. Property ‘fontScale’ is missing in type ‘import(“c:/Users/EDY/Desktop/xgl-pro/software/DevEco Studio/sdk/default/openharmony/ets/api/@ohos.app.ability.Configuration”).Configuration’ but required in type ‘Configuration’. <ArkTSCheck>

使用的api14,鸿蒙next5系统


更多关于HarmonyOS 鸿蒙Next状态栏深色适配的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

报错原因:

EntryAbility 继承自 UIAbility,但你在子类中重写 onConfigurationUpdate(newConfig: Configuration) 方法时,传入的 Configuration 类型 与父类方法声明中所使用的 Configuration 类型不完全相同

直接重写带了个参数过来,其实定义里面是没有参数的 类型没有导入过来,导入类型

cke_859.png

cke_11995.png

更多关于HarmonyOS 鸿蒙Next状态栏深色适配的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


正确的导入Configuration

import { Configuration } from '@ohos.app.ability.Configuration';

实现onConfigurationUpdate方法时应保持类型一致:

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';
import { Configuration } from '@ohos.app.ability.Configuration';

export default class EntryAbility extends UIAbility {
  onConfigurationUpdate(newConfig: Configuration): void {
    // 适配深色模式的核心代码
    if (newConfig.colorMode === Configuration.ColorMode.COLOR_MODE_DARK) {
      // 设置状态栏深色样式
      windowClass.setWindowSystemBarProperties({
        statusBarColor: '#000000',
        statusBarContentColor: '#FFFFFF'
      });
    }
  }
}

你少导入了一个包:Configuration,

导入一下包就可以了:

import { Configuration } from '@kit.AbilityKit';

把工程的 apiVersiontargetApiVersion 都改成跟系统一致的最新API,然后重新同步

HarmonyOS Next状态栏深色适配需使用系统提供的暗色主题配置能力。在config.json中配置"uiMode"为"dark",或动态调用AbilityContext的setUiMode(UiMode.DARK)。针对状态栏图标,需遵循系统暗色主题规范,使用白色或浅色系图标。状态栏文本颜色由系统自动适配,开发者无需额外处理,但需确保应用背景色与深色状态栏协调。WindowStage的setSystemBarProperties方法可单独设置状态栏属性。

根据报错信息分析,这是一个类型兼容性问题。问题出在onConfigurationUpdate方法的参数类型不匹配。

主要原因:

  1. 您使用的Configuration类型来自本地路径的SDK定义
  2. 但代码中期望的是标准@ohos.app.ability.Configuration类型

解决方案:

  1. 确保导入正确的Configuration类型:
import { Configuration } from '@ohos.app.ability';
  1. 修改方法签名为:
onConfigurationUpdate(newConfig: Configuration) {
    // 实现代码
}
  1. 如果仍报错,检查SDK版本是否与API14匹配,建议清理项目并重新构建。

这种类型不匹配问题在HarmonyOS Next开发中比较常见,主要是因为SDK路径引用和类型定义不一致导致的。

回到顶部