鸿蒙Arkts暗黑模式适配方案

一、在EntryAbility的onCreate()函数中添加下面代码:

AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', this.context.config.colorMode);

二、在EntryAbility中添加函数

onConfigurationUpdate(newConfig: Configuration): void {

    const currentColorMode: ConfigurationConstant.ColorMode | undefined = AppStorage.get('currentColorMode');

    if (currentColorMode !== newConfig.colorMode) {

      AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', newConfig.colorMode);

    }

  }

 

添加上面函数后,需要引入import { Configuration } from '@kit.AbilityKit';否则会报错。

 

三、在需要的页面添加下面代码:

 

 @StorageProp('currentColorMode') @Watch('onCurrentColorModeChange') currentColorMode: ConfigurationConstant.ColorMode =

    ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET;

  private isSystemDark: boolean = false;

 

四、添加函数

 

onCurrentColorModeChange(): void {

    if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {

      this.isSystemDark = false;

    } else if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK) {

      this.isSystemDark = true;

    }

  }

 

五、解释

 

isSystemDark 是我定义的变量。

 

 

 

评论列表 (7)

vian 发表于 2025-11-10 23:21:12
鸿蒙和安卓区别大吗:heo-被打:
林羽凡 发表于 2025-10-28 17:00:09
<a class="comment_at_admin">@ 秫米</a>是的,我也老是忘记,看来得弄个自动续期了:heo-被打:
林羽凡 发表于 2025-10-28 16:59:35
<a class="comment_at_admin">@ 我是军爸</a>是的哈,感谢提醒:heo-出家人:
林羽凡 发表于 2025-10-28 16:58:34
<a class="comment_at_admin">@ zeruns.</a>入门的话还是比较容易的
秫米 发表于 2025-10-24 10:53:16
:heo-被打: 只从免费证书又缩短时效后,我频繁提醒,大佬!你证书过期了!!!
我是军爸 发表于 2025-10-20 19:01:11
你的SSL证书到期了
zeruns. 发表于 2025-10-13 17:16:20
鸿蒙开发入门难吗