鸿蒙应用开发从入门到实战(十四):ArkUI组件Column&Row&线性布局

大家好,我是潘Sir,持续分享IT技术,帮你少走弯路。《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容、欢迎关注!

ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文主要讲解Column和Row组件的使用以及线性布局的方法。

一、Column&Row组件及线性布局

1.1 线性布局概述

​ 线性布局(LinearLayout)是开发中最常用的布局,可通过容器组件ColumnRow构建,其子组件会在垂直或者水平方向上进行线性排列,具体效果如下图所示

列排列

行排列

说明:

ColumnRow 容器均有两个轴线,分别是主轴交叉轴

  • 主轴 :线性布局容器在布局方向上的轴线,Row 容器主轴为横向Column 容器主轴为纵向
  • 交叉轴 :垂直于主轴方向的轴线。Row 容器交叉轴为纵向Column 容器交叉轴为横向

1.2 Column&Row参数

ColumnRow 容器的参数类型为{space?: string | number},开发者可通过space属性调整子元素在主轴方向上的间距,效果如下

列间隔

行间隔

示例代码:

pages目录下新建layout/linear目录,新建SpacePage.ets文件

typescript 复制代码
@Entry
@Component
// 线性布局
struct SpacePage {

  build() {
    // 通过space属性调整子元素在主轴方向上的间距
    Column({ space: 150 }) {
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)

      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)

      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)

    }.width('100%')
    .height('100%')
    .backgroundColor('#eeeeee')
    .justifyContent(FlexAlign.Center)
  }
}

1.3 常用属性

1.3.1 主轴方向排列方式

子元素沿主轴方向的排列方式可以通过justifyContent()方法进行设置,其参数类型为枚举类型FlexAlign,可选的枚举值有

示例代码:

pages/layout/linear目录,新建JustifyContent.ets文件

typescript 复制代码
@Entry
@Component
struct AlignItemsPage {

  build() {
    Column({ space: 2 }) {
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
    }.width('100%')
    .height('100%')
    // 子元素沿主轴方向的排列方式
    // .justifyContent(FlexAlign.Center)
    // .justifyContent(FlexAlign.Start)
    // .justifyContent(FlexAlign.End)
    // .justifyContent(FlexAlign.SpaceBetween)
    // .justifyContent(FlexAlign.SpaceAround)
    .justifyContent(FlexAlign.SpaceEvenly)
  }

}

1.3.2 交叉轴方向对齐方式

子元素沿交叉轴方向的对齐方式可以通过alignItems()方法进行设置,其参数类型,对于Column容器来讲是HorizontalAlign,对于Row容器来讲是VerticalAlign,两者都是枚举类型,可选择枚举值也都相同,具体内容如下

示例代码

pages/layout/linear目录,新建AlignItemsPage.ets文件

typescript 复制代码
@Entry
@Component
// 线性布局交叉轴排列方式
struct AlignItemsPage {

  build() {
    Column({ space: 2 }) {
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
      Row()
        .backgroundColor(Color.Green)
        .width('80%')
        .height(70)
    }.width('100%')
    .height('100%')
    // 子元素沿交叉轴方向的对齐方式
    // 对于Column容器来讲是HorizontalAlign,对于Row容器来讲是VerticalAlign,两者都是枚举类型,可选择枚举值也都相同
    // .alignItems(HorizontalAlign.Start)
    // .alignItems(HorizontalAlign.Center)
    .alignItems(HorizontalAlign.End)
  }
}

二、使用技巧

2.1 Blank组件

Blank 可作为ColumnRow容器的子组件,该组件不显示任何内容,并且会始终充满容器主轴方向上的剩余空间,效果如下:

示例代码:

拷贝icon_bluetooth.png到目录resources/base/media

pages/layout/linear目录下新建BlankPage.ets

typescript 复制代码
@Entry
@Component
struct BlankPage {

  build() {
    Column({ space: 50 }) {
      Row() {
        Image($r('app.media.icon_bluetooth'))
          .width(30)
          .height(30)
        Text('蓝牙')
          .fontSize(20)
          .margin({ left: 5 })
        Blank()
        Toggle({ type: ToggleType.Switch })
      }
      .width('90%')
      .height(60)
      .backgroundColor(Color.White)
      .padding({ left: 10, right: 10 })
      .borderRadius(15)
      .shadow({ radius: 10 })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#f2f2f2')
  }
}

2.2 layoutWeight属性

layoutWeight属性可用于ColumnRow 容器的子组件,其作用是配置子组件在主轴方向上的尺寸权重。配置该属性后,子组件沿主轴方向的尺寸设置(widthheight)将失效,具体尺寸根据权重进行计算,计算规则如下图所示:

示例代码:

pages/layout/linear目录下新建LayoutWeightPage.ets

typescript 复制代码
@Entry
@Component
  // layoutWeight属性
struct LayoutWeightPage {
  build() {
    // layoutWeight 配置子组件在主轴方向上的尺寸权重。
    // 配置该属性后,子组件沿主轴方向的尺寸设置(width或height)将失效,具体尺寸根据权重进行计算
    Column() {
      //Header:高度60vp
      Row() {
        Text('Header')
          .fontSize(30)
      }.backgroundColor('#66008000')
      .justifyContent(FlexAlign.Center)
      .height(60)
      .width('100%')

      //Content:充满剩余空间
      Row() {
        Text('Content')
          .fontSize(30)
      }
      .backgroundColor('#66ffa200')
      .justifyContent(FlexAlign.Center)
      .width('100%')
      // .height(200)
      .layoutWeight(3)

      // Footer:高度60vp
      Row() {
        Text('Footer')
          .fontSize(30)
      }
      .backgroundColor('#660e9fba')
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height(60)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#f2f2f2')
  }
}

《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容,防止迷路,欢迎关注!

相关推荐
想你依然心痛32 分钟前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“文思智脑“——PC端AI智能体沉浸式智能写作工作台
人工智能·ar·harmonyos·ai写作
小雨青年35 分钟前
鸿蒙 HarmonyOS 6 | Pura X Max 鸿蒙原生适配 09:展开态列表增加字段但不变复杂
华为·harmonyos
richard_yuu41 分钟前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
阿钱真强道5 小时前
24 鸿蒙LiteOS GPIO中断实战:从原理到上升沿/下降沿详解
harmonyos·中断·rk·liteos·开源鸿蒙·瑞芯微·rk2206
cd_949217217 小时前
鸿蒙系统下抖音存储空间不足怎么办?缓存清理教程
缓存·华为·harmonyos
轻口味9 小时前
HarmonyOS 6.1 全栈实战录 - 14 渲染树透镜:FrameNode 渲染状态感知与高性能 UI 调优实战
ui·华为·harmonyos
HwJack2010 小时前
HarmonyOS NEXT 游戏APP开发中如何正确拦截退出手势
游戏·华为·harmonyos
HwJack2010 小时前
HarmonyOS APP开发中ArkTS/JS 类型错误全景拆解
javascript·华为·harmonyos
lqj_本人11 小时前
鸿蒙PC:鸿蒙版本 Electron 框架环境搭建并且实现 XH 笔记应用
笔记·electron·harmonyos
不爱吃糖的程序媛11 小时前
特色软件 | 补齐 鸿蒙 PC 开发短板,Harmonybrew 的环境适配方案
华为·harmonyos