鸿蒙适应式布局和响应式布局零基础

案例解释1 GridRow + GridCol

c 复制代码
@Entry
@Component
struct DemoPage {
  build() {
    GridRow() {
      GridCol({ span: { sm: 12, md: 6, lg: 4 } }) {
        Text('左边内容')
          .width('100%')
          .height(120)
      }

      GridCol({ span: { sm: 12, md: 6, lg: 8 } }) {
        Text('右边内容')
          .width('100%')
          .height(120)
      }
    }
  }
}
  1. span就代表了这一个GridCol所占的列数 在鸿蒙布局 默认共有12列

    当当前列数占用超出total row之后 就会换行

  2. .width('100%') .height(120)

    这两个写法有什么区别?

  • '100%'是让大小跟着父容器走 父容器变大 他也会跟着变大
  • 具体值写法是想让他大小固定 大小不会跟着父容器走
  1. sm md lg到底是什么?
    他们就相当于窗口宽度的标签 实际开发可以用这个办法来判断应该选择哪个大小
c 复制代码
let windowWidthVp = windowWidth / display.getDisplayByIdSync(displayId).densityPixels

if (windowWidthVp < 320) {
  newBp = 'xs'
} else if (windowWidthVp < 600) {
  newBp = 'sm'
} else if (windowWidthVp < 840) {
  newBp = 'md'
} else {
  newBp = 'lg'
}
  • 小于 320vp,算 xs
  • 320 到 600vp 左右,算 sm
  • 600 到 840vp 左右,算 md
  • 大于等于 840vp,算 lg
  1. GridRow和GridCol和正常的row和column也有一些区别
    Grid~他是有自动换行的 正常的row和column会直接挤出去
    举个例子
c 复制代码
@Entry
@Component
struct DemoPage {
  build() {
    Row() {
      Column() {
        Text('左边内容')
          .width('60%')
          .height('30%')
      }

      Column() {
        Text('右边内容')
          .width('30%')
          .height('30%')
      }
    }
  }
}

案例解释2 layoutWeight

这个就相当于划分权重 差不多也是按比例分配界面吧

c 复制代码
@Entry
@Component
struct WeightDemo {
  build() {
    Row() {
      Text('固定')
        .width(80)
        .height(50)

      Text('自适应1')
        .layoutWeight(1)
        .height(50)
        .fontSize(50)

      Text('自适应2')
        .layoutWeight(2)
        .height(50)
        .fontSize(50)
    }
    .width('100%')
  }
}

我们可以看到不同的权重的界面拥有的大小不同 显示上也有很多差异

方案三 displayPriority (此文章未完善)

用在画面发生变化时隐藏某些界面

displayPriority设置优先级 优先级越高(数字越大) 越先隐藏

c 复制代码
@Entry
@Component
struct Index {
  build() {
    Row() {
      Text('标题')
        .displayPriority(3)
        .margin(20)

      Text('副标题')
        .displayPriority(2)
        .margin(20)

      Text('更多')
        .displayPriority(1)
        .margin(20)
    }
    .width('100%')
  }
}

方案四 Flex 的 wrap

华为官方 Flex 文档说明,Flex 用于排列、对齐、分配剩余空间;而多行流式排布,通常就配合换行来做。官方旧版相关容器说明里也明确有 wrap / nowrap 这类设置。

  1. 效果:
  • 一行放得下就一行
  • 放不下自动到下一行
  1. 这个很适合:
  • 标签组
  • 按钮组
  • 筛选条件区
  • 小卡片流式排布
c 复制代码
@Entry
@Component
struct WrapDemo {
  build() {
    Flex({ wrap: FlexWrap.Wrap }) {
      ForEach([1, 2, 3, 4, 5, 6, 7, 8], (item: number) => {
        Text(`标签${item}`)
          .padding(10)
          .margin(6)
      })
    }
    .width('100%')
  }
}
  1. 解释一下这个foreach吧
c 复制代码
      ForEach([1, 2, 3, 4, 5, 6, 7, 8], (item: number) => {
        Text(`标签${item}`)
        }

其实就相当于

cpp 复制代码
for (int i = 0; i < 3; i++) {
    // 创建一个组件
}

${item}是把变量转化为能在字符串里输出的样子

Image($r('app.media.xxx'))

是加载图片的 用的是相对路径

绝对路径用法

Image('resources/base/media/a.png')

相关推荐
李游Leo35 分钟前
《HarmonyOS 7 ArkGraphics 3D 空间设计开发实战》08:动画、生命周期与SpaceRoom工程化收尾【鸿蒙心迹】
3d·harmonyos
轻口味1 小时前
HarmonyOS 7 新特性2:音频编创——轻音台里的降噪、环绕与格式转换
华为·音视频·harmonyos·鸿蒙·音频编创
m0_738185822 小时前
Flutter 鸿蒙化实战:flutter_blue_plus 适配 OpenHarmony,蓝牙扫描连接开箱即用
flutter·华为·harmonyos·鸿蒙
李游Leo2 小时前
HarmonyOS 7 + ArkUI + Adaptive Layout 学习笔记:折叠屏多形态布局适配与窗口状态响应机制【鸿蒙心迹】
笔记·学习·harmonyos
贾伟康2 小时前
【HarmonyOS 7新能力|067】LTPO可变帧率实战:让动画流畅而静态页面省电
harmonyos·arkts·低功耗·ltpo·可变帧率
LucianaiB2 小时前
华为云码道 AI 编程实战:我用 CodeArts 智能体打造了一款 HarmonyOS 游戏化专注应用「智办 ZhiBan」
人工智能·ai·华为云·harmonyos·codearts·材料
李游Leo3 小时前
HarmonyOS 7 + ArkTS + Image Kit 学习笔记:图像超分处理链路与 PixelMap 数据流实践【鸿蒙心迹】
笔记·学习·harmonyos
xq95273 小时前
JSON to ArkTS Model 王者归来
harmonyos
m0_738185823 小时前
Flutter 鸿蒙化实战:flutter_background_service 适配 OpenHarmony,后台服务
flutter·华为·harmonyos·鸿蒙
less_121383 小时前
HarmonyOS WPS Open SDK 快速入门:HAR 集成到 sendRequest 打开文档
华为·harmonyos·wps