HarmonyOS 如何设置Colum默认显示在Scorll组件的顶部?

Scroll组件中的Colum组件为什么总是在中间显示?只有Colum的内容需要滑动时,才会顶头显示。如何设置Colum默认显示在Scorll组件的顶部

阅读 426
1 个回答

给Scroll添加属性align(Alignment.Top)即可。如下示例所示:

@Entry
@Component
struct Index {
  scroller: Scroller = new Scroller;
  private arr: number[] = [0, 1, 2, 3, 4]

  build() {
    Scroll(this.scroller) {
      Column() {
        ForEach(this.arr, (item: number) => {
          Text(item.toString())
            .width('90%')
            .height(100)
            .backgroundColor(0xFFFFFF)
            .borderWidth(1)
            .borderColor(Color.Black)
            .borderRadius(15)
            .fontSize(16)
            .textAlign(TextAlign.Center)
        }, (item: string) => item)
      }.width('100%').backgroundColor(0xDCDCDC)
    }
    .align(Alignment.Top)
    .backgroundColor(Color.Yellow)
    .height('100%')
    .edgeEffect(EdgeEffect.Spring)
    .scrollSnap({
      snapAlign: ScrollSnapAlign.START,
      snapPagination: 400,
      enableSnapToStart: true,
      enableSnapToEnd: true
    })
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进