HarmonyOS开发:ArkUI线性布局(Column/Row)完全解析

线性布局

线性布局指的是在布局容器内,子组件呈水平或者垂直线性方向排列。垂直布局是Column组件、水平布局是Row组件。

线性布局通过两个轴线(主轴和交叉轴)作为参考基准,用于确定子组件在容器内的对齐方式。

下面详细介绍一下ColumnRow的使用

水平布局(Row)

排列方向

Row线性布局子元素默认从左到右排列。可以使用direction来设置排列方向。

ts 复制代码
Row() {
      Row().width(50).height(50).backgroundColor(Color.Red)
      Row().width(50).height(50).backgroundColor(Color.Green)
      Row().width(50).height(50).backgroundColor(Color.Yellow)
    }
    .width(250)
    .borderWidth(1)
    .margin(10)
    //.direction(Direction.Ltr)	//从左到右(默认效果)


Row() {
      Row().width(50).height(50).backgroundColor(Color.Red)
      Row().width(50).height(50).backgroundColor(Color.Green)
      Row().width(50).height(50).backgroundColor(Color.Yellow)
    }
    .width(250)
    .borderWidth(1)
    .margin(10)
    .direction(Direction.Rtl)	//从右到左

元素间隔

通过Row({space: 10})可以调整字元素之间的间隔。space数字越大间距越大。

ts 复制代码
Row({space:10}) {
    Text('1').width(50).height(50).backgroundColor(Color.Red)
          .textAlign(TextAlign.Center)
     
    Text('2').width(50).height(50).backgroundColor(Color.Green)
                .textAlign(TextAlign.Center)
    
    Text('3').width(50).height(50).backgroundColor(Color.Yellow)
          .textAlign(TextAlign.Center)
}
.width(250)
.borderWidth(1)
.margin(10)

对齐方式

Row对齐方式分为主轴对齐方式和交叉轴对齐方式,属性方法如下。

主轴对齐方式

  • .justifyContent(value: FlextAlign) 主轴对齐方式(水平方向),一共6种对齐方式根据需要选择。
    • 默认左对齐 FlexAlign.Start
ts 复制代码
 Row({space:10}) {
  Text('1').width(50).height(50).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  Text('2').width(50).height(50).backgroundColor(Color.Green).textAlign(TextAlign.Center)
  Text('3').width(50).height(50).backgroundColor(Color.Yellow).textAlign(TextAlign.Center)
}
.width(250)
.height(100)
.borderWidth(1)
.margin(10)
.justifyContent(FlexAlign.SpaceEvenly)	//平均对齐

交叉轴对齐方式

.alignItems(value: VerticalAlign) 交叉轴对齐方式(垂直方向),一共3种对齐方式,根据需要选择。

  • 默认垂直居中对齐VerticalAlign.Center
ts 复制代码
Row({space:10}) {
  Text('1').width(50).height(50).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  Text('2').width(50).height(50).backgroundColor(Color.Green).textAlign(TextAlign.Center)
  Text('3').width(50).height(50).backgroundColor(Color.Yellow).textAlign(TextAlign.Center)
}
.width(250)
.height(100)
.borderWidth(1)
.margin(10)
.justifyContent(FlexAlign.SpaceEvenly)
.alignItems(VerticalAlign.Top)	//上对齐

垂直布局(Column)

排列方向

Column线性布局子元素默认从上到下排列。可以使用.reverse(true)来设置排列方向反转。

ts 复制代码
Column() {
  Text('1').width(50).height(50).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  Text('2').width(50).height(50).backgroundColor(Color.Green).textAlign(TextAlign.Center)
  Text('3').width(50).height(50).backgroundColor(Color.Yellow).textAlign(TextAlign.Center)
}
.width(100)
.height(250)
.borderWidth(1)
.margin(10)
.reverse(true)  //反转(从下到上排列)

元素间隔

通过Column({space: 10})可以调整字元素之间的间隔。space数字越大间距越大。

ts 复制代码
Column({space:10}) {
    Text('1').width(50).height(50).backgroundColor(Color.Red)
          .textAlign(TextAlign.Center)
     
    Text('2').width(50).height(50).backgroundColor(Color.Green)
                .textAlign(TextAlign.Center)
    
    Text('3').width(50).height(50).backgroundColor(Color.Yellow)
          .textAlign(TextAlign.Center)
}
.width(250)
.borderWidth(1)
.margin(10)

对齐方式

Cloumn对齐方式分为主轴对齐方式和交叉轴对齐方式(垂直方向),属性方法如下。

主轴对齐方式

  • .justifyContent(value: FlextAlign) 主轴对齐方式,一共6种对齐方式根据需要选择。
    • 默认左对齐 FlexAlign.Start
ts 复制代码
 Cloumn({space:10}) {
  Text('1').width(50).height(50).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  Text('2').width(50).height(50).backgroundColor(Color.Green).textAlign(TextAlign.Center)
  Text('3').width(50).height(50).backgroundColor(Color.Yellow).textAlign(TextAlign.Center)
}
.width(250)
.height(100)
.borderWidth(1)
.margin(10)
.justifyContent(FlexAlign.SpaceEvenly)	//平均对齐

交叉轴对齐方式

.alignItems(value: HorizontalAlign) 交叉轴对齐方式(水平方向),一共3种对齐方式,根据需要选择。

  • 默认水平居中对齐HorizontalAlign.Center
ts 复制代码
Row({space:10}) {
  Text('1').width(50).height(50).backgroundColor(Color.Red).textAlign(TextAlign.Center)
  Text('2').width(50).height(50).backgroundColor(Color.Green).textAlign(TextAlign.Center)
  Text('3').width(50).height(50).backgroundColor(Color.Yellow).textAlign(TextAlign.Center)
}
.width(250)
.height(100)
.borderWidth(1)
.margin(10)
.justifyContent(FlexAlign.SpaceEvenly)
.alignItems(HorizontalAlign.Start)	//左对齐

填充空白

在线性布局中可以使用Blank来填充空白区域,让ColumnRow的子元素能自适应使用不同尺寸。

ts 复制代码
Column() {
  Row() {
    Text('蓝牙')
    Blank()
    Toggle({ type: ToggleType.Switch, isOn: true })
  }
  .width("90%")
  .borderWidth(1)
  .borderRadius(15)
  .padding(5)
}.width('100%')
.height('100%')

比例分配

在线性布局中ColumnRow,都可以在主轴方向上,按照比例分配子组件的尺寸;

  • 通过给子组件添加.layoutWeight(value: number) 属性实现。
    • value:该组件所占比例的份数。
ts 复制代码
Row() {
    Row().width(50).height(50).backgroundColor(Color.Red)
      .layoutWeight(1)	//占1份

    Row().width(50).height(50).backgroundColor(Color.Green)
      .layoutWeight(2)  //占2份

    Row().width(50).height(50).backgroundColor(Color.Yellow)
      .layoutWeight(3)  //占3份
}
.width(300)
.borderWidth(1)

如图所示:

  • 如果在Row或者Column只有一个组件添加了.layoutWeight(1) 则表示剩余空间全给该组件。
ts 复制代码
Row() {
    Row().width(50).height(50).backgroundColor(Color.Red)
    Row().width(50).height(50).backgroundColor(Color.Green)
    Row().width(50).height(50).backgroundColor(Color.Yellow)
      .layoutWeight(3)
}
.width(300)
.borderWidth(1)

如图所示:


到此,线性布局ColumnRow的用法就学完了。撒花🎉🎉🎉🎉

相关推荐
键盘鼓手苏苏14 小时前
Flutter 三方库 p2plib 的鸿蒙化适配指南 - 实现高性能的端到端(P2P)加密通讯、支持分布式节点发现与去中心化数据流传输实战
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx14 小时前
Flutter for OpenHarmony:postgrest 直接访问 PostgreSQL 数据库的 RESTful 客户端(Supabase 核心驱动) 深度解析与鸿蒙适配指南
数据库·flutter·华为·postgresql·restful·harmonyos·鸿蒙
加农炮手Jinx14 小时前
Flutter 组件 heart 适配鸿蒙 HarmonyOS 实战:分布式心跳监控,构建全场景保活检测与链路哨兵架构
flutter·harmonyos·鸿蒙·openharmony
钛态14 小时前
Flutter 三方库 http_mock_adapter — 赋能鸿蒙应用开发的高效率网络接口 Mock 与自动化测试注入引擎(适配鸿蒙 HarmonyOS Next ohos)
android·网络协议·flutter·http·华为·中间件·harmonyos
王码码203514 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
王码码203514 小时前
Flutter 三方库 dns_client 的鸿蒙化适配指南 - 告别 DNS 劫持、探索 DNS-over-HTTPS (DoH) 技术、构建安全的鸿蒙网络请求环境
flutter·harmonyos·鸿蒙·openharmony·dns_client
键盘鼓手苏苏14 小时前
Flutter 组件 highlighter 适配鸿蒙 HarmonyOS 实战:高性能语法高亮,构建大规模代码分析与文本染色架构
flutter·harmonyos·鸿蒙·openharmony
国医中兴14 小时前
Flutter 三方库 langchain_google 的鸿蒙化适配指南 - 链接 Gemini 智慧中枢、LangChain AI 实战、鸿蒙级智能应用专家
flutter·langchain·harmonyos
左手厨刀右手茼蒿14 小时前
Flutter for OpenHarmony: Flutter 三方库 shamsi_date 助力鸿蒙应用精准适配波斯历法(中东出海必备)
android·flutter·ui·华为·自动化·harmonyos
雷帝木木14 小时前
Flutter 三方库 http_client_interceptor 的鸿蒙化适配指南 - 实现原生 HttpClient 的全量请求拦截、支持端侧动态 Headers 注入与网络流量审计实战
flutter·harmonyos·鸿蒙·openharmony·http_client_interceptor