HarmonyOS 如何获取当前是否为深色模式?

需要根据当前是否为深色模式设置不同的颜色。

阅读 627
1 个回答

a等于0时为深色,1时为浅色,可以参考下面demo:

import { common, ConfigurationConstant } from '@kit.AbilityKit';

@Entry
@Component
struct Page7235 {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('Page7235HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          let context = getContext(this) as common.UIAbilityContext
          let a: ConfigurationConstant.ColorMode | undefined = context.config.colorMode
          console.log(a?.toString())
        })
    }
    .height('100%')
    .width('100%')
  }
}

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-app-ability-configuration-V5\#%E5%B1%9E%E6%80%A7

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