除了已有的px,vp,fp,lpx外,有没有类似CSS中1vw表示1%的视窗宽度

除了已有的px,vp,fp,lpx外,有没有类似CSS中1vw表示1%的视窗宽度。

HarmonyOS
2024-06-03 23:12:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zhuyanxi

可通过获取窗口大小后,再取窗口大小的百分比进行设置。

import window from '@ohos.window'; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = "100" 
  @State windowWidth: number = 0; 
 
  aboutToAppear() { 
    window.getLastWindow(getContext(this), (err, data) => { 
      this.windowWidth = data.getWindowProperties().windowRect.width; 
    }) 
  } 
 
  getVW(vw: number) { 
    if (vw >= 0 && vw <= 100) { 
      return this.windowWidth * vw / 100 
    } 
    return 0; 
  } 
 
  build() { 
    Column() { 
      Text(this.message) 
        .height(100) 
        .fontColor(Color.White) 
        .width(px2vp(this.getVW(40))) 
        .backgroundColor(Color.Black) 
    } 
    .width('100%') 
    .height("100%") 
  } 
}
分享
微博
QQ
微信
回复
2024-06-04 22:08:51
相关问题
vpfppx区别,有谁知道?
724浏览 • 1回复 待解决
pxvp之间如何相互转换
584浏览 • 1回复 待解决
有没有接口能获取到组件宽度
337浏览 • 1回复 待解决
鸿蒙有没有类似viewpage组件
6371浏览 • 1回复 已解决
鸿蒙有没有类似carplay应用
6447浏览 • 1回复 待解决
c++ 有vp2px 接口么?
441浏览 • 1回复 待解决
JS ui支持vp、字体大小支持fp了吗?
4607浏览 • 1回复 待解决
DevEcoStudio2.2 Beta1模拟器
6167浏览 • 2回复 已解决
有谁知道有没有类似lodash库
631浏览 • 1回复 待解决