当前位置:   article > 正文

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

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

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

cke_747.png

cke_1138.png

  1. 代码实现
    全部代码如下
    1. import window from '@ohos.window';
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State statusBarHeight:number=-1
    6. public click(){
    7. window.getTopWindow((err, data) => {
    8. var windowClass = data;
    9. var type = window.AvoidAreaType.TYPE_SYSTEM;
    10. windowClass.getAvoidArea(type, (err, data) => {
    11. if (err) {
    12. console.error('Failed to obtain the area. Cause:' + JSON.stringify(err));
    13. return;
    14. }
    15. this. statusBarHeight = data.topRect.height;
    16. console.log("statusBarHeight====>>>>"+this.statusBarHeight);
    17. });
    18. });
    19. }
    20. build() {
    21. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
    22. Text('获取状态栏高度')
    23. .fontSize(50)
    24. .fontWeight(FontWeight.Bold)
    25. .onClick(this.click.bind(this))
    26. Text('状态栏高度'+this.statusBarHeight)
    27. .fontSize(50)
    28. .margin(10)
    29. .fontColor(Color.Red)
    30. }
    31. .width('100%')
    32. .height('100%')
    33. }
    34. }

2.运行效果
效果如下

cke_6030.png

欲了解更多更全技术文章,欢迎访问:https://developer.huawei.com/consumer/cn/forum/topic/0203803170703380031?fid=0102683795438680754?ha_source=zzh

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/578884
推荐阅读
  

闽ICP备14008679号