HarmonyOS 单页面如何设置进入修改状态栏颜色,返回(或者侧滑返回)再次复原状态栏颜色

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考示例:

import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
import { router } from '@kit.ArkUI';

@Component
@Entry
struct index2 {
  private windowClass = AppStorage.get("windowClass") as window.Window
  @State message: string = 'Hello World';

  //进入页面
  aboutToAppear(): void {

    //设置状态栏颜色
    let sysBarProps: window.SystemBarProperties = {
      statusBarColor: Color.Red.toString(),
      statusBarContentColor: '#00ff00',
    };
    this.windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
      let 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.');
    });
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.hideStatusBar()
        })
    }
    .height('100%')
    .width('100%')
  }

  onBackPress() {
    this.hideStatusBar()
    return true
  }

  hideStatusBar() {
    let sysBarProps: window.SystemBarProperties = {
      statusBarColor: Color.White.toString(),
      statusBarContentColor: Color.Black.toString(),
    };
    this.windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
      let 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.');
    });
    router.back()
  }
}
import router from '@ohos.router';

@Entry
@Component
struct Index1 {
  build() {
    Row() {
      Column() {

        Text('click')
          .onClick(() => {

            router.pushUrl({
              url: 'pages/Index2'
            })
          }).fontSize(40)
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 状态栏颜色如何修改
69浏览 • 1回复 待解决
HarmonyOS 如何设置状态栏颜色
34浏览 • 1回复 待解决
HarmonyOS 动态设置状态栏颜色
44浏览 • 1回复 待解决
HarmonyOS 状态栏图标颜色设置
43浏览 • 1回复 待解决
HarmonyOS 状态栏怎么设置颜色
81浏览 • 1回复 待解决
HarmonyOS 修改状态栏颜色不生效
333浏览 • 1回复 待解决
鸿蒙怎么 修改状态栏字体颜色
12503浏览 • 1回复 待解决
如何设置状态栏和导航颜色
3005浏览 • 1回复 待解决