多个Ability并适配深浅色模式/系统语言切换

多个Ability并适配深浅色模式/系统语言切换

HarmonyOS
2024-05-26 15:11:05
380浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
makeer

使用的核心API

核心代码解释

从Ability中获取上下文,及配置信息存入工具类ContextUtil中

export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
    ContextUtil.mContext = this.context; 
    ContextUtil.mConfiguration = this.context.config; 
  } 
 
  onConfigurationUpdate(newConfig: Configuration): void { 
    ContextUtil.mConfiguration = newConfig; 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

根据当前模式来切换深浅模式

@Entry 
@Component 
struct Index { 
  @State darkMode: boolean = ContextUtil.mConfiguration.colorMode == ConfigurationConstant.ColorMode.COLOR_MODE_DARK; 
  build() { 
    Row() { 
      Column() { 
        Row(){ 
          Text($r('app.string.dark_mode')) 
          Toggle({ type: ToggleType.Switch, isOn: this.darkMode }).onChange((isOn:Boolean)=>{ 
            let applicationContext = ContextUtil.mContext.getApplicationContext(); 
            if (this.darkMode) { 
              //设置系统浅色模式。 
              this.darkMode = false; 
              applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) 
            }else { 
              //设置系统深色模式。 
              this.darkMode = true; 
              applicationContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK) 
            } 
          }) 
        }.height(200) 
        Text($r('app.string.default_app')) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

适配的版本信息

IDE版本:DevEco Studio 4.1.1.300

SDK版本:4.1.3.5

分享
微博
QQ
微信
回复
2024-05-27 20:12:17


相关问题
app适配深色浅色切换模式
2630浏览 • 1回复 待解决
HarmonyOS 如何监听深浅色模式
675浏览 • 1回复 待解决
HarmonyOS 主动设置深浅色模式
851浏览 • 1回复 待解决
HarmonyOS 浅色模式与深色模式
902浏览 • 1回复 待解决