前言
在开发过程中,公司的设计师为公司Harmony App产品,准备了独立的Token的情况下,App内基本上所有的Page页面的背景颜色均为一致的。这时候不应该在每个页面中的最外层布局上添加
backgroundColor
属性。使用backgroundColor
属性来设置页面的背景颜色,缺点过于明显。在于,当在未来的某天公司希望为App的颜色上大改时,页面的背景颜色需要再每一个页面上进行修改,这使得工作量增加、遗漏的概率大等问题。
使用
使用ArkUI中对于ArkTS下的窗口管理来进行开发,使用setWindowBackgroundColor
方法来进行修改窗口颜色,使得整体页面不需要再依次修改背景颜色。
示例代码
typescript
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/WelcomePage', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
// 1. 获取应用主窗口
let windowClass = windowStage.getMainWindowSync();
// 2. 设置窗口颜色
windowClass.setWindowBackgroundColor('#F4F4F4')
});
}
}
总结
在华为的HarmonyOS开发API参考中,window窗口管理相关API还有很多,大家可以自行参考学习,本文章只提到了作者遇到的问题时所做的记录。
谢谢大家浏览~ ~ !