HarmonyOS 如何按Component页面设置status bar的背景和文字颜色

比如A页面是红色的头,而router跳转到B页面的时候需要白色的头。

HarmonyOS
2024-12-26 15:43:11
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

参考demo:

1、工具类GlobalContext。

export class GlobalContext {
  private constructor() { }
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();
  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }
  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }
  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

2、EntryAbility获取窗口实例。

onWindowStageCreate(windowStage: window.WindowStage): void {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  windowStage.loadContent('pages/Index', (err, data) =&gt; {
  if (err.code) {
  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  return;
}
//获取窗口实例,并将窗口实例封装到工具类
let windowClass: window.Window | undefined = undefined;
try {
  window.getLastWindow(this.context, (err: BusinessError, data) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
      return;
    }
    windowClass = data;
    GlobalContext.getContext().setObject("window",windowClass)
    console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); });
} catch (exception) {
  console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}
  • 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.

3、在index页面设置状态栏状态。

import { BusinessError } from '@ohos.base';

let SystemBarProperties: window.SystemBarProperties = {
  statusBarColor: '#ff00ff',
  navigationBarColor: '#00ff00',
  //以下两个属性从API Version8开始支持
  statusBarContentColor: '#ffffff',
  navigationBarContentColor: '#00ffff'
};
try {
  windowClass.setWindowSystemBarProperties(SystemBarProperties, (err: BusinessError) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
      return;
    }
    console.info('Succeeded in setting the system bar properties.');
  });
} catch (exception) {
  console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(exception));
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

关于setWindowSystemBarProperties,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowsystembarproperties9-1

分享
微博
QQ
微信
回复
2024-12-26 17:31:09
相关问题
HarmonyOS Tabs组件bar背景设置问题
1375浏览 • 1回复 待解决
文字背景颜色渐变显示
2876浏览 • 1回复 待解决
HarmonyOS 如何设置SubWindow背景颜色
569浏览 • 1回复 待解决
HarmonyOS windows级别页面设置
582浏览 • 1回复 待解决
HarmonyOS 如何对page页面设置透明
1540浏览 • 1回复 待解决
XComponent组件如何设置背景颜色
3020浏览 • 1回复 待解决
如何设置子窗口背景颜色
931浏览 • 1回复 待解决
如何页面设置为深色模式
3056浏览 • 1回复 待解决
如何进行主入口页面设置
284浏览 • 0回复 待解决
如何背景颜色设置为透明
3620浏览 • 1回复 待解决
如何实现页面背景颜色置灰
1431浏览 • 1回复 待解决