鸿蒙自适应布局的一些方法 以及使用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 拉伸 都可以来做自适应布局

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

相关推荐
特立独行的猫a20 分钟前
HarmonyOS 鸿蒙PC三方库移植:vcpkg方式的 Port 脚本编写简明教程
华为·harmonyos·openharmony·vcpkg·三方库移植
Ww.xh1 小时前
鸿蒙Flutter混合开发实战:跨平台UI无缝集成
flutter·华为·harmonyos
chenbin___1 小时前
鸿蒙RN position: ‘absolute‘ 和 zIndex 的兼容性问题(转自千问)
前端·javascript·react native·harmonyos
苏杰豪2 小时前
DevEco Studio 启动鸿蒙模拟器提示未开启 Hyper-V,怎么处理?
华为·harmonyos
chenjixue2 小时前
记录下我理解的安卓,鸿蒙,ios, rn , fullter, Jetpack Compose,react 的相似与不同
android·华为·harmonyos
想你依然心痛3 小时前
HarmonyOS 6(API 23)悬浮导航与沉浸光感实战:打造下一代玻璃拟态UI体验
ui·华为·harmonyos·悬浮导航·沉浸光感
廖松洋(Alina)12 小时前
【果一下】鸿蒙APP上架了
华为·harmonyos
Lanren的编程日记13 小时前
Flutter鸿蒙应用开发:生物识别(指纹/面容)功能集成实战
flutter·华为·harmonyos
Lanren的编程日记17 小时前
Flutter鸿蒙应用开发:基础UI组件库设计与实现实战
flutter·ui·harmonyos
chenbin___21 小时前
鸿蒙(HarmonyOS)支持 useNativeDriver的详细说明(转自千问)
前端·javascript·react native·react.js·harmonyos