HarmonyOS鸿蒙Next中应用主动设置深浅色模式
HarmonyOS鸿蒙Next中应用主动设置深浅色模式 应用主动设置深浅色模式应该在哪个文件里设置?如何设置?能否固定状态栏颜色?
3 回复
可以参考下下面的代码看看
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT);
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
windowClass = data;
let SystemBarProperties: window.SystemBarProperties = {
statusBarColor: '#ffffff',
};
try {
windowClass.setWindowSystemBarProperties(SystemBarProperties, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeeded in setting the system bar properties.');
});
} catch (exception) {
console.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
}
});
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 {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}
更多关于HarmonyOS鸿蒙Next中应用主动设置深浅色模式的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS(鸿蒙)Next中,应用可以通过AppearanceManager类来主动设置深浅色模式。开发者可以使用setSystemAppearanceMode方法来切换系统的外观模式。该方法接受一个参数,可以是AppearanceMode.LIGHT(浅色模式)或AppearanceMode.DARK(深色模式)。具体实现如下:
- 引入相关模块:
import appearance from '@ohos.app.ability.appearance';
- 设置深浅色模式:
appearance.setSystemAppearanceMode(appearance.AppearanceMode.LIGHT); // 设置为浅色模式
appearance.setSystemAppearanceMode(appearance.AppearanceMode.DARK); // 设置为深色模式
- 获取当前模式:
let currentMode = appearance.getSystemAppearanceMode();
通过上述方法,应用可以根据用户需求或特定场景动态切换深浅色模式,提升用户体验。
在HarmonyOS鸿蒙Next中,应用可以通过Configuration类来主动设置应用的深浅色模式。具体步骤如下:
-
获取当前配置: 使用
Resources.getSystem().getConfiguration()获取当前的配置对象。 -
修改UI模式: 通过
Configuration.uiMode属性,设置UI_MODE_NIGHT_YES(深色模式)或UI_MODE_NIGHT_NO(浅色模式)。 -
应用配置: 使用
Resources.updateConfiguration()方法应用新的配置。
示例代码:
Configuration config = getResources().getConfiguration();
config.uiMode = (isDarkMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
通过这种方式,应用可以根据用户需求或系统设置动态切换深浅色模式。

