
回复
在鸿蒙开发中,信息流与列表的弹性布局是跨设备适配的关键。本文解析Grid+List组合方案,助你打造流畅的多端信息流体验~
@Entry
@Component
struct FeedLayout {
@State data = Array(10).fill($r('app.media.img'))
build() {
GridRow() {
GridCol() {
List() {
ForEach(data, (item) => {
ListItem() {
Grid() {
GridItem() {
Image(item)
.width('100%')
.aspectRatio(0.8) // 固定宽高比
.constraintSize({ minWidth: 180, maxWidth: 300 })
Text('信息流内容').fontSize(16).padding(10)
}
}
}
})
}
.lanes({ minLength: 300, maxLength: 360 }) // 动态列宽
.padding(12)
}
}
.onBreakpointChange(() => { /* 断点适配逻辑 */ })
}
}
属性 | 作用 | 适配场景 |
---|---|---|
lanes |
动态设置列宽范围 | 大屏自动多列,小屏单列 |
aspectRatio |
固定图片宽高比 | 避免大屏图片拉伸变形 |
constraintSize |
限制元素尺寸范围 | 防止极端尺寸下的布局错乱 |
aspectRatio(0.8)
保持信息流卡片统一视觉比例padding(16)
,小屏保持padding(8)
LazyForEach
itemRenderStrategy: ItemRenderStrategy.Virtual
@Builder
封装复用组件设备类型 | 布局效果 | 核心配置 |
---|---|---|
手机 | 单列垂直排列 | lanes({ minLength: 300 }) |
平板 | 双列自适应布局 | lanes({ maxLength: 360 }) |
.lanes({
minLength: this.breakpoint === 'sm' ? 300 : 360,
maxLength: this.breakpoint === 'sm' ? 320 : 400
})
aspectRatio
锁定视觉比例,避免变形lanes
属性实现大屏多列、小屏单列的自动切换