鸿蒙Scroll布局,横向与纵向

注意,当横向scroll时,直接子元素的宽,不能100%,

当纵向scroll时,直接子元素的高,不能100%

1、纵向代码:

方法1:用数值计算,来设置中间的高度:

复制代码
@Entry
@Component
struct ScrollCase {
  @State message: string = 'Hello World';
  @State middleHeigh: number = 0;

  build() {

    Column() {
      Row() {

      }
      .height(50)
      .width('100%')
      .backgroundColor(Color.Blue)

      Column(){

      }
      .width('100%')
      .height(this.middleHeigh)
      .backgroundColor(Color.Orange)

      Row() {

      }
      .height(50)
      .width('100%')
      .backgroundColor(Color.Red)
    }
    .justifyContent(FlexAlign.SpaceBetween) //两端对齐
    .width('100%')
    .height('100%')
    .onAreaChange((old: Area, newArea: Area) => { //界面渲染时会被调用
      this.middleHeigh=(newArea.height as number) - 100
    })
  }
}

效果:

完全效果:

代码:

复制代码
@Entry
@Component
struct ScrollCase {
  @State message: string = 'Hello World';
  @State middleHeigh: number = 0;
  scroller:Scroller =new Scroller()
  build() {

    Column() {
      Row() {

      }
      .height(50)
      .width('100%')
      .backgroundColor(Color.Blue)
      .onClick(()=>{
        this.scroller.scrollEdge(Edge.Bottom)
      })

      Scroll(this.scroller){
        Column(){
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
          ScrollItem()
        }
      }
      .scrollBarColor(Color.Red)

      .width('100%')
      .height(this.middleHeigh)
      .backgroundColor(Color.Orange)

      Row() {
      }
      .height(50)
      .width('100%')
      .backgroundColor(Color.Red)
      .onClick(()=>{
        this.scroller.scrollEdge(Edge.Top)
      })
    }
    .justifyContent(FlexAlign.SpaceBetween) //两端对齐
    .width('100%')
    .height('100%')
    .onAreaChange((old: Area, newArea: Area) => { //界面渲染时会被调用
      this.middleHeigh=(newArea.height as number) - 100
    })
  }
}

@Component
struct ScrollItem {
  build() {
    Row(){
      Text("滚动区域内容")
        .backgroundColor(Color.White)
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height(80)
    .backgroundColor(Color.Pink)
    .margin(10)
  }
}

Scroller对Scorll进行控制滑动到顶、或底

2、横向滚动,效果:

代码:

复制代码
@Entry
@Component
struct ScrollCase02 {
  @State message: string = 'Hello World';
  scroller: Scroller = new Scroller()

  build() {
    Column() {
      Scroll(this.scroller) {
        Row({ space: 20 }) {
          Actor()
          Actor()
          Actor()
          Actor()
          Actor()
          Actor()
          Actor()
          Actor()
          Actor()

        }
        .height(200)
        .backgroundColor(Color.Orange)
      }
      .height(200)
      .width('100%')
      .backgroundColor(Color.Pink)
      .scrollable(ScrollDirection.Horizontal)
      Row() {
        Button("滚动左边")
          .onClick(() => {
            this.scroller.scrollEdge(Edge.Start)
          })
        Button("滚动右边")
          .onClick(() => {
            this.scroller.scrollEdge(Edge.End)
          })
      }

    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
    .backgroundColor(Color.Pink)



  }
}

@Component
struct Actor {
  build() {
    Row() {
      Text("热辣滚烫--贾玲")
        .fontColor(Color.White)
    }
    .width(100)
    .height(180)
    .backgroundColor(Color.Red)
  }
}
相关推荐
waeng_luo1 天前
[鸿蒙2025领航者闯关] ArkUI动画实战
鸿蒙·鸿蒙2025领航者闯关·鸿蒙6实战·开发者年度总结
遇到困难睡大觉哈哈1 天前
Harmony os —— Data Augmentation Kit 知识问答完整示例实战拆解(从 0 跑通流式 RAG)
harmonyos·鸿蒙
鸿蒙开发工程师—阿辉1 天前
HarmonyOS5 极致动效实验室:基本动画的使用
harmonyos·arkts·鸿蒙
kirk_wang1 天前
Flutter Printing库在OpenHarmony上的适配实战
flutter·移动开发·跨平台·arkts·鸿蒙
kirk_wang2 天前
Flutter `video_player`库在鸿蒙端的视频播放优化:一份实用的适配指南
flutter·移动开发·跨平台·arkts·鸿蒙
汉堡黄•᷄ࡇ•᷅2 天前
鸿蒙开发:案例集合List:多列表相互拖拽(删除/插入,偏移动效)(微暇)
华为·harmonyos·鸿蒙·鸿蒙系统
waeng_luo2 天前
[鸿蒙2025领航者闯关]使用RelationalStore实现增删改查(CRUD)操作
harmonyos·鸿蒙·#鸿蒙2025领航者闯关·#鸿蒙6实战
后端小张2 天前
【鸿蒙2025领航者闯关】从技术突破到生态共建,开发者的成长与远航
华为·wpf·生态·harmonyos·鸿蒙·鸿蒙系统·鸿蒙2025领航者试炼
kirk_wang2 天前
Flutter 三方库鸿蒙适配手记:让 `flutter_isolate` 在 OpenHarmony 上跑起来
flutter·移动开发·跨平台·arkts·鸿蒙
kirk_wang3 天前
Flutter path_provider 在 OpenHarmony 平台上的实现与适配实践
flutter·移动开发·跨平台·arkts·鸿蒙