【HarmonyOS】ArkUI - 列表布局(List)

List

列表(List)是一种复杂容器,具备下列特点:

  • 列表项(ListItem)数量过多超出屏幕后,会自动提供滚动功能。
  • 列表项(ListItem)既可以纵向排列,也可以横向排列。

语法

List({ space: 10 }) {
  ForEach([1, 2, 3, 4], item => {
    ListItem() {
      Text('ListItem')
    }
  })
}
.width('100%')
.listDirection(Axis.Vertical)
  • space:列表项间距。

  • ListItem:列表项。它本身不是一个容器,代表 List 内部的一个项,需要把各种布局组件(Text、Button 等)写在 ListItem 里。因为 ListItem 内部只能包含一个根组件,所以如果要写多个组件,需要将组件包到 Row 或 Colum 容器里。

    ListItem() {
      Row() {
        ...
      }
    }
    
  • listDirection:列表方向,默认纵向。

    Axis.Vertical   // 纵向
    Axis.Horizontal // 横向
    

示例代码

class Item {
  name: string
  image: ResourceStr
  box_office: string

  constructor(name: string, image: ResourceStr, box_office: string) {
    this.name = name
    this.image = image
    this.box_office = box_office
  }
}
@Entry
@Component
struct Index {
  private items: Array<Item> = [
    { name: '热辣滚烫', image: $r('app.media.1'), box_office: '23.41亿' },
    { name: '飞驰人生2', image: $r('app.media.2'), box_office: '20.46亿' },
    { name: '熊出没·逆转时空', image: $r('app.media.3'), box_office: '11.82亿' },
    { name: '第二十条', image: $r('app.media.4'), box_office: '10.41亿' },
    { name: '我们一起摇太阳', image: $r('app.media.5'), box_office: '9618.7万' },
    { name: '红毯先生', image: $r('app.media.6'), box_office: '7884.5万' }
  ]

  build() {
    Column({ space: 8 }) {
      Row() {
        Text('2024春节档新片票房榜')
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
      }

      List({ space: 8 }) {
        ForEach(
          this.items,
          (item: Item) => {
            ListItem() {
              Row({ space: 8 }) {
                Image(item.image)
                  .width(157)
                  .height(220)
                Column() {
                  Text(item.name)
                    .fontSize(20)
                    .fontWeight(FontWeight.Bold)
                  Text(item.box_office)
                    .fontSize(18)
                }
                .height('100%')
                .alignItems(HorizontalAlign.Start)
              }
              .width('100%')
              .height(220)
            }
          }
        )
      }
      .width('100%')
      .height('100%')
    }
    .width('100%')
    .height('100%')
    .padding(8)
  }
}

运行效果:
列表布局

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tyhoo Wu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值