HarmonyOS 如何设置应用显示模式固定为浅色模式

按照文档提示使用:

this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK)

的方式去设置应用显示模式没有起作用,一直是跟随系统的显示模式。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

应用主动设置深浅色模式,即颜色只固定不跟随系统模式变动。监听深浅色切换事件参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-app-ability-configuration-V5

参考示例如下:

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');
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 如何设置固定浅色模式
95浏览 • 1回复 待解决
HarmonyOS 主动设置浅色模式
25浏览 • 1回复 待解决
HarmonyOS 浅色模式与深色模式
22浏览 • 1回复 待解决
HarmonyOS 如何监听深浅色模式
11浏览 • 1回复 待解决
应用如何适配大显示模式
2004浏览 • 1回复 待解决
app适配深色浅色切换模式
1900浏览 • 1回复 待解决