zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Harmony OS】【ARK UI】js或ETS如何获取状态栏高度

HarmonyOSJSUI 如何 获取 高度 状态栏
2023-09-11 14:17:17 时间

 在Harmony OS项目怎么获取状态高度?
在开发中我们可以参考窗口的getAvoidArea和getTopWindow的api,分为“代码实现”,“运行效果”两个步骤进行实现

cke_747.png

cke_1138.png

 

  1. 代码实现
    全部代码如下
    import window from '@ohos.window';
    @Entry
    @Component
    struct Index {
      @State statusBarHeight:number=-1
      public  click(){
        window.getTopWindow((err, data) => {
          var windowClass = data;
          var type = window.AvoidAreaType.TYPE_SYSTEM;
          windowClass.getAvoidArea(type, (err, data) => {
            if (err) {
              console.error('Failed to obtain the area. Cause:' + JSON.stringify(err));
              return;
            }
            this. statusBarHeight = data.topRect.height;
            console.log("statusBarHeight====>>>>"+this.statusBarHeight);
          });
        });
      }
      build() {
        Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
          Text('获取状态栏高度')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
          .onClick(this.click.bind(this))
    
          Text('状态栏高度'+this.statusBarHeight)
            .fontSize(50)
            .margin(10)
          .fontColor(Color.Red)
        }
        .width('100%')
        .height('100%')
      }
    }

2.运行效果
效果如下

cke_6030.png