鸿蒙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)
  }
}
相关推荐
世人万千丶1 小时前
鸿蒙日志体系高级应用:HiLog分级输出/隐私脱敏/远程日志采集/线上问题精准溯源方案
学习·harmonyos·鸿蒙
世人万千丶9 小时前
鸿蒙Crash高级捕获与异常监控:全局异常兜底/崩溃栈解析/符号表还原/智能聚类/闭环修复
学习·机器学习·华为·数据挖掘·harmonyos·鸿蒙·聚类
超爱西西鸭14 小时前
鸿蒙大型项目高级模块化拆分:高内聚低耦合架构设计/HSP动态交付/接口契约/依赖倒置落地规范
学习·华为·harmonyos·鸿蒙
超爱西西鸭17 小时前
鸿蒙企业级CI/CD高级工程化搭建:自动化构建流水线/多环境配置/自动化测试集成/签名打包发布
android·学习·ci/cd·华为·自动化·harmonyos·鸿蒙
aqi003 天前
鸿蒙版本的小小机器人APP开放源码啦
人工智能·华为·harmonyos·鸿蒙·harmony
lmy_loveF3 天前
华为模拟器添加卡片
华为·鸿蒙
lmy_loveF3 天前
华为DevEco Studio安装指南:一步到位
鸿蒙
lmy_loveF3 天前
HarmonyOS开发入门:DevEco Studio安装指南
华为·鸿蒙
FF2501_940228583 天前
HarmonyOS应用开发实战:猫猫大作战-在排行榜和历史记录查询中,排序是必不可少的操作【apple_product_name】
华为·harmonyos·鸿蒙
echohelloworld115 天前
HarmonyOS应用开发实战:猫猫大作战-KV Store 跨端数据同步
harmonyos·鸿蒙