#鸿蒙通关秘籍#如何在HarmonyOS NEXT中使用Grid布局实现电影列表

HarmonyOS
2024-12-02 14:08:04
893浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
墨s幻想P6P

在HarmonyOS NEXT开发中,可以使用Grid和GridItem组件来创建网格布局,适合用于展示类似电影列表的界面。下面的代码展示了如何使用Grid组件创建一个电影列表:

Grid() {
  ForEach(this.arr, (item: string) => {
    GridItem() {
      Column() {
        Image(item)
          .height(140)
          .width('100%')
          .objectFit(ImageFit.Cover)
        Text("电影《默杀》宁波福州")
          .fontSize(13)
          .margin({ top: 6 })
          .textOverflow({
            overflow:TextOverflow.Clip
          })
          .maxLines(1)
      }.width('100%')
    }
  })
}
.columnsTemplate('1fr 1fr 1fr')
.margin({ top: 20 })
.columnsGap(10)
.rowsGap(10)
.margin(10)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

代码中使用了Grid()组件来定义网格布局,其中通过ForEach方法循环渲染多个GridItem。每个GridItem中使用了Column布局包含了一张图片和一个标题文本。columnsTemplate('1fr 1fr 1fr')用于定义每行显示三列,支持自适应宽度。通过columnsGaprowsGap来定义列与列之间和行与行之间的间距。

分享
微博
QQ
微信
回复
2024-12-02 16:58:16


相关问题