鸿蒙状态栏控制

569 阅读2分钟

鸿蒙状态栏控制

前言

鸿蒙文章写完这篇就差不多了吧,国庆休息下,开始新的阶段。这个是状态栏相关的代码,虽然官方文档都有,感觉整理到一起的话还是有的用的。

获取 WindowStage

和窗口有关的东西,都需要WindowStage来处理,前面切屏的文章我也写了,不过这里再写下。

这里要到UIAbility获取windowStage,比如我这在EntryAbility中:

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    AppStorage.setOrCreate('windowStage', windowStage);
    
    // 。。。
    
  }

保存到AppStorage,然后其他地方就可也获取了:

  windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage;
  mainWin: window.Window = this.windowStage.getMainWindowSync();

全屏

拿到windowStage后,设置全屏很简单:

  public fullScreen(isFull: boolean) {
    this.mainWin.setWindowLayoutFullScreen(isFull)
      .then(() => {
        console.info('Succeeded in setting the window layout to full-screen mode.');
      })
      .catch((err: BusinessError) => {
        console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
      });

  }

获取状态栏高度

有时候要空出状态栏高度,可以通过下面方法获取值:

 public async getWindowAvoidArea(context: Context): Promise<number> {
    try {
      const mainWindow = await window.getLastWindow(context);
      const avoidAreaType = window.AvoidAreaType.TYPE_SYSTEM; // 系统默认区域,包括状态栏,导航栏
      const avoidArea = mainWindow.getWindowAvoidArea(avoidAreaType);
      const height = avoidArea.topRect.height;
      return px2vp(height)
    } catch (e) {
      console.log('getWindowAvoidArea fail');
      return null
    }
  }

隐藏、显示状态栏

隐藏状态栏和导航栏:

  public immersive(): void {
    this.mainWin.setWindowLayoutFullScreen(true)
    
    let names: Array<'status' | 'navigation'> = [];
    this.mainWin.setWindowSystemBarEnable(names, (err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in setting the system bar to be visible.');
    });

  }

显示状态栏和导航栏:

  public disImmersive(): void {
    this.mainWin.setWindowLayoutFullScreen(false)

    let names: Array<'status' | 'navigation'> = ['status'];
    this.mainWin.setWindowSystemBarEnable(names, (err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in setting the system bar to be visible.');
    });
  }

设置状态栏颜色

public setStatusBarColor(color: string) {
    let sysBarProps: window.SystemBarProperties = {
      statusBarColor: color,
    };
    this.mainWin.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.');
    });
  }

设置状态栏字体颜色

public setStatusBarContentColor(color: string) {
    // let color = "#000000"
    let sysBarProps: window.SystemBarProperties = {
      statusBarContentColor: color
    };
    this.mainWin.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.');
    });
  }

禁止录屏

API被废弃了,不过好像还能用:

  public securityMode(isPrivacy: boolean) {
    this.mainWin.setPrivacyMode(isPrivacy).then(() => {
    }).catch((error: BusinessError) => {
      console.log(error.message)
    })
  }

小结

整理了一些和鸿蒙状态栏、窗口相关的方法,包含了全屏、隐藏状态栏、设置状态栏及文字颜色、禁止录屏等。