应用如何切换夜间模式

OpenHarmony设置深浅色模式指导

场景

应用设置夜间模式。

HarmonyOS
2024-06-11 20:55:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
depengli

实现步骤

1.配置深色模式的资源目录,使用分层参数隔离深浅色模式的样式

2.设置系统深色模式

注意:需要系统级签名,并在module.json5中配置"ohos.permission.UPDATE_CONFIGURATION"权限

完整实例代码

resources/base/element/color.json

{ 
  "color": [ 
    { 
      "name": "start_window_background", 
      "value": "#FFFFFF" 
    } 
  ] 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

resources/dark/element/color.json

{ 
  "color": [ 
    { 
      "name": "start_window_background", 
      "value": "#000000" 
    } 
  ] 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

ets:

import uiAppearance from '@ohos.uiAppearance' 
 
@Entry 
@Component 
struct NightMode { 
  @State message: string = '浅色模式' 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) //获取当前的深色模式配置 
          .fontSize(40) 
          .fontWeight(FontWeight.Bold) 
          .backgroundColor($r('app.color.start_window_background')) 
          .onClick(() => { 
            if (uiAppearance.getDarkMode() == 0) {//获取当前的深色模式配置。 
              //设置系统浅色模式。 
              uiAppearance.setDarkMode(uiAppearance.DarkMode.ALWAYS_LIGHT).then(() => { 
                console.log('Set dark-mode successfully.'); 
                this.message = '浅色模式' 
              }).catch((err) => { 
                console.log(`wangyi Set dark-mode failed, ${err}`); 
              }); 
 
            } else { 
              //设置系统深色模式。 
              uiAppearance.setDarkMode(uiAppearance.DarkMode.ALWAYS_DARK).then(() => { 
                console.log('Set dark-mode successfully.'); 
                this.message='深色模式' 
              }).catch((err) => { 
                console.log(`Set dark-mode failed, ${err}`); 
              }); 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .width('100%') 
    .height('100%') 
    .backgroundColor($r('app.color.start_window_background'))//不同模式下,不同背景色 
  } 
}
  • 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.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

效果图


分享
微博
QQ
微信
回复
2024-06-12 22:49:09


相关问题
关于切换深色模式应用自动重启应用
6930浏览 • 2回复 待解决
app适配深色浅色切换模式
2217浏览 • 1回复 待解决
应用如何适配深色模式
1780浏览 • 1回复 待解决
HarmonyOS 组件切换深色模式不生效
912浏览 • 1回复 待解决
应用如何适配深色模式
2931浏览 • 1回复 待解决
HarmonyOS 如何监听前台应用切换
538浏览 • 1回复 待解决
HarmonyOS 深色模式切换后界面不刷新
627浏览 • 1回复 待解决
应用如何适配大显示模式
2389浏览 • 1回复 待解决
Component如何监听应用前后台切换
1868浏览 • 1回复 待解决
HarmonyOS 如何实现应用内的语言切换
640浏览 • 1回复 待解决