import { display } from '@kit.ArkUI'; // 获取设备屏幕的宽度和高度 getScreenWH() { let displayClass: display.Display | null = null; displayClass = display.getDefaultDisplaySync(); // 获取屏幕宽度,并转换成VP console.info(`The screen width is ${this.getUIContext().px2vp(displayClass.width)}vp.`); // 获取屏幕高度,并转换成VP console.info(`The screen height is ${this.getUIContext().px2vp(displayClass.height)}vp.`); }
// 例如此页面的总高度就是Text的高度200vp+Column的高度500vp=700vp build() { Column(){ Text(this.message) .backgroundColor(Color.Gray) .height('200vp') Column(){ } .backgroundColor(Color.Green) .height('500vp') } }
// 例如此页面的总宽度就是3个Row的宽度和,100vp+200vp+100vp=400vp build() { Column(){ Row(){ Row() .width('100vp') Row() .width('200vp') Row() .width('100vp') } Text(this.message) .backgroundColor(Color.Gray) .height('200vp') Column(){ } .backgroundColor(Color.Green) .height('500vp') } }
针对页面总宽度或总高度超过屏幕的总宽度或总高度,可以在部分组件外包一层Scroll组件,通过滚动的方式在有限的界面区域显示出更多的内容。
竖直方向和水平方向显示不全后通过Scroll组件解决:
@Entry @Component struct ScrollDemo { build() { Column() { // 此处无论Scroll子组件Column组件高度有多高,显示区域均只占Scroll同级别组件高度的40% Scroll() { Column() { ForEach([0, 1, 2, 3, 4, 5], (item: number) => { Text(item.toString()) .width('90%') .height(150) .backgroundColor(0xFFFFFF) .borderRadius(15) .fontSize(16) .textAlign(TextAlign.Center) .margin({ top: 10 }) }, (item: string) => `vertical${item}`) } .padding({ left: 16, right: 16 }) } .height('40%') .backgroundColor(Color.Gray) // 此处无论Scroll子组件Text的宽度有多宽,均可通过横向滑动展示 Scroll() { Row() { ForEach([0, 1, 2, 3, 4, 5], (item: number) => { Text(item.toString()) .width('90%') .height('90%') .backgroundColor(0xFFFFFF) .borderRadius(15) .fontSize(16) .textAlign(TextAlign.Center) .margin({ left: 10, top: 10 }) }, (item: string) => `horizontal${item}`) } .justifyContent(FlexAlign.SpaceAround) } .height('40%') // 设置滚动方向为水平方向 .scrollable(ScrollDirection.Horizontal) .backgroundColor(Color.Gray) } .justifyContent(FlexAlign.SpaceAround) .height('100%') } }