鸿蒙自适应布局的一些方法 以及使用Slider 控制宽度

均分效果:

代码:

在代码中 使用了Slider 滑块 来控制测试组件的宽度大小

scss 复制代码
@Entry
@Component
struct Index {
  @State rate: number = 300

  // 底部滑块
  @Builder
  sliderBuilder() {
    Slider({
      value: $$this.rate,
      min: 200,
      max: 600,
      style: SliderStyle.OutSet
    })
      .blockColor(Color.White)
      .width('60%')

      .position({ x: '20%', y: '80%' })
  }

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      // 标记现在的宽度
      Text('宽度:' + this.rate.toFixed(0))
        .margin({top:50})
        .fontSize(42)
        .zIndex(2)
        .translate({ x: 20, y: 20 })
        .fontColor(Color.Orange)

      Column() {
        Column() {
          Row() {
            ForEach(Array.from({ length: 4 }), (item:string,index: number) => {
              Column() {
                Text(index+1+'')
                  .fontSize(22)
                  .fontColor(Color.White)
              }
              .justifyContent(FlexAlign.Center)
              .border({ width: 2, color: Color.Blue })
              .width(50)
              .height(50)
              .backgroundColor(Color.Red)
            })

          }
          .justifyContent(FlexAlign.SpaceEvenly) // 均分
          .width(this.rate) // 绑定滑块改变的尺寸
          .padding({ top: 16 })
          .backgroundColor('#FFFFFF')
          .borderRadius(16)
          //下方滑块
          this.sliderBuilder()
        }
        .width('100%')
        .height('100%')
        .backgroundColor('#F1F3F5')
        .justifyContent(FlexAlign.Center)
        .alignItems(HorizontalAlign.Center)
      }
    }
  }
}

隐藏效果:

代码: 第46行 .displayPriority(index) // 布局优先级

displayPriority:当父容器空间不足的时候, 低优先级的组件会被隐藏

scss 复制代码
@Entry
@Component
struct Index {
  @State rate: number = 300

  // 底部滑块
  @Builder
  sliderBuilder() {
    Slider({
      value: $$this.rate,
      min: 200,
      max: 600,
      style: SliderStyle.OutSet
    })
      .blockColor(Color.White)
      .width('60%')

      .position({ x: '20%', y: '80%' })
  }

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      // 标记现在的宽度
      Text('宽度:' + this.rate.toFixed(0))
        .margin({top:50})
        .fontSize(42)
        .zIndex(2)
        .translate({ x: 20, y: 20 })
        .fontColor(Color.Orange)

      Column() {
        Column() {
          Row() {
            ForEach(Array.from({ length: 6 }), (item:string,index: number) => {
              Column() {
                Text(index+1+'')
                  .fontSize(22)
                  .fontColor(Color.White)
              }
              .justifyContent(FlexAlign.Center)
              .border({ width: 2, color: Color.Blue })
              .width(60)
              .height(50)
              .backgroundColor(Color.Red)
              .displayPriority(index) // 布局优先级
            })

          }
          // .justifyContent(FlexAlign.SpaceEvenly) // 均分
          .width(this.rate) // 绑定滑块改变的尺寸
          .padding({ top: 16 })
          .backgroundColor('#FFFFFF')
          .borderRadius(16)
          //下方滑块
          this.sliderBuilder()
        }
        .width('100%')
        .height('100%')
        .backgroundColor('#F1F3F5')
        .justifyContent(FlexAlign.Center)
        .alignItems(HorizontalAlign.Center)
      }
    }
  }
}

还有一些: layoutWeight占比权重 / 百分比布局 / aspectRatio缩放 / FlexWrap.Wrap 折行 / flexGrow 和 flexShrink 拉伸 都可以来做自适应布局

如果想要更好的适配多个适配,还是使用响应式布局更好点

相关推荐
Jalor29 分钟前
Flutter + 鸿蒙 | Flutter 跳转鸿蒙原生界面
flutter·harmonyos
zhanshuo1 小时前
开发者必看!如何在HarmonyOS中快速调用摄像头功能
harmonyos
HMSCore1 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
zhanshuo1 小时前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
HarmonyOS小助手2 小时前
闯入鸿蒙:浪漫、理想与「草台班子」
harmonyos·鸿蒙·harmonyos next·鸿蒙生态
xq95272 小时前
flutter 鸿蒙化插件开发横空出世
harmonyos
HarmonyOS_SDK2 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
别说我什么都不会3 小时前
【OpenHarmony】鸿蒙开发之Jmustache
harmonyos
塞尔维亚大汉3 小时前
鸿蒙内核源码分析(文件系统篇) | 用图书管理说文件系统
源码·harmonyos
塞尔维亚大汉7 小时前
鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗
源码·harmonyos