HarmonyOS鸿蒙Next中如何给应用设置主题色(类似遇到重大事情时一键置灰,一键置红)

HarmonyOS鸿蒙Next中如何给应用设置主题色(类似遇到重大事情时一键置灰,一键置红) 如题:目前没有在相关技术材料上找到可以实现这个功能的途径,有大佬已经实现了或者能提供思路嘛,感蟹

14 回复

灰化接口

grayscale

样例

Column {
   ......
}.grayscale(1) //1 : 代表置灰

common.d.ts

/**
 * Adds a grayscale effect to the current component.
 * The value is the gray scale conversion ratio. If the input parameter is 1.0, the gray scale image is completely converted to the gray scale image. If the input parameter is 0.0, the image does not change.
 * If the input parameter is between 0.0 and 1.0, the effect changes. (Percentage)
 * @form
 * @since 9
 */
grayscale(value: number): T;

更多关于HarmonyOS鸿蒙Next中如何给应用设置主题色(类似遇到重大事情时一键置灰,一键置红)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


赞,

基本信息
姓名:张三
职位:软件工程师
技能:Python, Java, C++

姓名:李四
职位:产品经理
技能:产品管理, 用户研究, 市场分析

根布局设置置灰接口参数为1,就可以全局置灰。

  1. 自己开发一个特定的主题,,这个比较low
  2. 等官方更新,设置窗口颜色过滤之类的,,一键切换。

主题是不是只能是暗黑模式这种,感觉也达不到效果。
我看iOS13上是有这个官方系统接口,且已经实现的,

所以需要等官方api更新呗,或者去openharmony库提issue或者pr,

颜色值不用常量,改用变量不就行啦。

我想实现的是整个页面实现黑白效果,包括页面上的各个文字图片,有点像比如逝世的时候黑色效果这中,不是说单个组件。

加个半透明的灰色蒙层?

不行啊,只是整体颜色变淡了,但是呈现不出黑白效果。

在HarmonyOS鸿蒙Next中,给应用设置主题色可以通过ResourceManagerThemeManager来实现。首先,在resources目录下定义主题色资源,例如在colors.json中定义灰色和红色:

{
  "color": {
    "gray_theme": "#808080",
    "red_theme": "#FF0000"
  }
}

然后在themes.json中定义主题:

{
  "themes": [
    {
      "name": "GrayTheme",
      "parent": "DefaultTheme",
      "colors": {
        "primary_color": "$color:gray_theme"
      }
    },
    {
      "name": "RedTheme",
      "parent": "DefaultTheme",
      "colors": {
        "primary_color": "$color:red_theme"
      }
    }
  ]
}

在代码中,使用ThemeManager动态切换主题:

import theme from '@ohos.theme';

// 切换为灰色主题
theme.setTheme('GrayTheme');

// 切换为红色主题
theme.setTheme('RedTheme');

通过这种方式,可以实现应用主题色的动态切换,包括一键置灰或一键置红。

在HarmonyOS鸿蒙Next中,可以通过ResourceManager动态设置应用主题色。首先在resources目录下定义不同主题的color.json文件,如gray_theme.jsonred_theme.json。然后使用ResourceManagerupdateConfiguration方法切换主题。例如:

ResourceManager resourceManager = getResourceManager();
Configuration config = new Configuration();
config.setTheme("gray_theme"); // 或 "red_theme"
resourceManager.updateConfiguration(config, null);
回到顶部