HarmonyOS 鸿蒙Next系统颜色模式获取

HarmonyOS 鸿蒙Next系统颜色模式获取 听说可以建一个不显示的1x1的web组件去获取系统颜色状态,然后调用,能实现深色模式跟随系统,是真的不?有佬有实现的代码吗?

2 回复

使用Environment设备环境查询,里面有系统 色彩模型类型。

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-environment-0000001473537710-V2

更多关于HarmonyOS 鸿蒙Next系统颜色模式获取的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)系统中,获取颜色模式可以通过Configuration类实现。具体步骤如下:

  1. 获取当前配置:使用getResources().getConfiguration()获取当前设备的配置信息。
  2. 检查颜色模式:通过configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK判断当前是否为夜间模式。

示例代码:

Configuration configuration = getResources().getConfiguration();
int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
boolean isNightMode = nightMode == Configuration.UI_MODE_NIGHT_YES;

此方法可用于动态调整应用主题,以适应用户的偏好设置。

回到顶部