在Harmony OS项目怎么获取状态高度?
在开发中我们可以参考窗口的getAvoidArea和getTopWindow的api,分为“代码实现”,“运行效果”两个步骤进行实现
- 代码实现
全部代码如下
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.运行效果
效果如下
欲了解更多更全技术文章,欢迎访问:https://developer.huawei.com/consumer/cn/forum/topic/0203803170703380031?fid=0102683795438680754?ha_source=zzh |