Column({ space: 5 }) {
Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%')
// flexGrow()表示剩余空间分配给该元素的比例
Flex() {
Text('flexGrow(2)')
.flexGrow(2) // 父容器分配给该Text的宽度为剩余宽度的2/3
.height(100)
.backgroundColor(0xF5DEB3)
.textAlign(TextAlign.Center)
Text('flexGrow(1)')
.flexGrow(1) // 父容器分配给该Text的宽度为剩余宽度的1/3
.height(100)
.backgroundColor(0xD2B48C)
.textAlign(TextAlign.Center)
}.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
}.width('100%').margin({ top: 5 })
Column({ space: 5 }) {
Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%')
// flexShrink()表示该元素的压缩比例,基于超出的总尺寸进行计算
// 第一个text压缩比例是0,另外两个都是1,因此放不下时等比例压缩后两个,第一个不压缩
Flex({ direction: FlexDirection.Row }) {
Text('flexShrink(0)')
.flexShrink(0) // 不压缩,总是占据50%的宽度
.width('50%')
.height(100)
.backgroundColor(0xF5DEB3)
.textAlign(TextAlign.Center)
Text('default flexShrink') // 默认值为1, 压缩,占据25%宽度
.width('40%')
.height(100)
.backgroundColor(0xD2B48C)
.textAlign(TextAlign.Center)
Text('flexShrink(1)')
.flexShrink(1) // 压缩,占据25%宽度
.width('40%')
.height(100)
.backgroundColor(0xF5DEB3)
.textAlign(TextAlign.Center)
}.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
}.width('100%').margin({ top: 5 })
Column() {
Row() {
ForEach(this.list, (item:number) => {…})
}.width('100%’)
// 均匀分配父容器主轴方向的剩余空间
.justifyContent(FlexAlign.SpaceEvenly)
// 同上Row
Row() {...}
}
.width(this.rate * 100 + '%')
Row() {
Column() {…}
.layoutWeight(1) // 设置子组件在父容器主轴方向的布局权重
Column() {…}
.layoutWeight(1) // 设置子组件在父容器主轴方向的布局权重
Column() {…}
.layoutWeight(1) // 设置子组件在父容器主轴方向的布局权重
}
.width(this.rate * 100 + '%')
Column() {
Column() {
Image($r("app.media.illustrator")).width('100%').height('100%')
}
.aspectRatio(1) // 固定宽高比
}
.height(this.sliderHeight)
.width(this.sliderWidth)
Row({ space: 10 }) {
// 通过List组件实现自动延伸能力
List({ space: 10 }) {...}
.listDirection(Axis.Horizontal)
.width('100%')
}
.width(this.rate * 100 + '%')
Row() {
Image($r("app.media.favorite"))
.displayPriority(1) // 布局优先级
Image($r("app.media.down"))
.displayPriority(2) // 布局优先级
Image($r("app.media.pause"))
.displayPriority(3) // 布局优先级
Image($r("app.media.next"))
.displayPriority(2) // 布局优先级
Image($r("app.media.list"))
.displayPriority(1) // 布局优先级
}
.width(this.rate * 100 + '%')
Column() {
// 通过Flex组件warp参数实现自适应折行
Flex({
wrap: FlexWrap.Wrap,
direction: FlexDirection.Row
}) {
ForEach(this.imageList, (item:Resource) => {
Image(item).width(183).height(138)
})
}
.width(this.rate * 100 + '%')
}
断点名称 | 取值范围(vp) | 设备描述 |
---|---|---|
xs | [0, 320) | 最小宽度类型设备。 |
sm | [320, 520) | 小宽度类型设备。 |
md | [520, 840) | 中等宽度类型设备。 |
lg | [840, +∞) | 大宽度类型设备。 |
鸿蒙系统中,栅格的默认列数12列,可以根据不同断点设置不同的列数(Column),设置不同的间隔(Gutter),设置栅格子组件占据的栅格容器列数。
事件:onBreakpointChange在断点发生变化时触发,可以针对不同的断点进行个性化定制开发,例如针对超大屏幕固定显示某个组件等。