HarmonyOS Next ArkTs布局入门——线性布局Row、Column(01)

412 阅读14分钟

环境及工具

HarmonyOS 5.0.0 Release
DevEcoStudio
适用于HarmonyOS Next原生开发

1、概述

布局指用特定的组件或者属性来管理用户页面所放置UI组件的大小和位置。组件按照布局的要求依次排列,构成应用的页面。

在声明式UI中,所有的页面都是由自定义组件构成,开发者可以根据自己的需求,选择合适的布局进行页面开发。

一个页面开发中,最优先的就是确定UI的布局结构,布局的结构通常是分层级的,代表了用户界面中的整体架构。一个常见的页面结构如下所示:

3434.jpg

为实现上述效果,开发者需要在页面中声明对应的元素。其中,Page表示页面的根节点,Column/Row等元素为系统组件(线性布局组件)。

ArkTS提供了9种常见的布局,开发者可根据实际应用场景选择合适的布局进行页面开发:

  • 线性布局(Row / Column)
  • 弹性布局(Flex)
  • 层叠布局(Stack)
  • 相对布局(RelativeContainer)
  • 栅格布局(GridRow、GridCol)
  • 媒体查询(@ohos.mediaquery)
  • 列表(List)
  • 网格(Grid)
  • 轮播(Swipper)

接下来,我们先来接触一下最常用的两个布局:线性布局 & 弹性布局。

2、线性布局

线性布局(LinearLayout)是开发中最常用的布局,通过线性容器Row和Column构建。

线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column容器内子元素按照垂直方向排列,Row容器内子元素按照水平方向排列。

2.1、基本概念

  • 布局容器: 具有布局能力的容器组件,可以承载其他元素作为其子元素,布局容器会对其子元素进行尺寸计算和布局排列。
  • 布局子元素: 布局容器内部的元素。
  • 主轴: 线性布局容器在布局方向上的轴线,子元素默认沿主轴排列。Row容器主轴为水平方向,Column容器主轴为垂直方向。
  • 交叉轴: 垂直于主轴方向的轴线。Row容器交叉轴为垂直方向,Column容器交叉轴为水平方向。
  • 间距: 布局子元素的间距。

2.2、布局子元素在排列方向上的间距

在布局容器内,可以通过space属性设置排列方向上子元素的间距,使各子元素在排列方向上有等间距效果。

  • Column容器内排列方向的间距图
Column({ space: 20 }) {  
    Text('space: 20')
        .fontSize(15)
        .fontColor(Color.Gray)
        .width('90%')  
    Row()
        .width('90%')
        .height(50)
        .backgroundColor('#9EC4E7')  
    Row()
        .width('90%')
        .height(50)
        .backgroundColor('#9EC4E7')
    Row()
        .width('90%')
        .height(50)
        .backgroundColor('#9EC4E7')
}
.width('100%')

556.png

  • Row容器内排列方向的间距图
Row({ space: 50 }) {
  Text('space: 50')
    .fontSize(15)
    .fontColor(Color.Gray)
  Row().width('10%')
    .height(150)
    .backgroundColor('#9EC4E7')
  Row().width('10%')
    .height(150)
    .backgroundColor('#9EC4E7')
  Row().width('10%')
    .height(150)
    .backgroundColor('#9EC4E7')
}
.width('90%')

568.png

2.3、布局子元素在交叉轴上的对齐方式

在布局容器内,可以通过alignItems属性设置子元素在交叉轴(排列方向的垂直方向)上的对齐方式。其中,交叉轴为垂直方向时,取值为VerticalAlign类型,水平方向取值为HorizontalAlign。

alignSelf属性用于控制单个子元素在容器交叉轴上的对齐方式,其优先级高于alignItems属性,如果设置了alignSelf属性,则在单个子元素上会覆盖alignItems属性。

  • Column容器内子元素在水平方向上的排列

HorizontalAlign.Start:子元素在水平方向左对齐

@Entry
@Component
struct Index{
  build() {
    Column() {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50).backgroundColor('#9EC4E7')
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
    }
    .height(200)
    .width('100%')
    // 设置水平方向左对齐
    .alignItems(HorizontalAlign.Start)
    .backgroundColor('#DE2710')
  }
}

101.png

HorizontalAlign.Center:子元素在水平方向居中对齐。

@Entry
@Component
struct Index{
  build() {
    Column() {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50).backgroundColor('#9EC4E7')
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
    }
    .height(200)
    .width('100%')
    // 设置水平方向居中对齐
    .alignItems(HorizontalAlign.Center)
    .backgroundColor('#DE2710')
  }
}

102.png

HorizontalAlign.End:子元素在水平方向右对齐。

@Entry
@Component
struct Index{
  build() {
    Column() {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50).backgroundColor('#9EC4E7')
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor('#9EC4E7')
    }
    .height(200)
    .width('100%')
    // 设置水平方向居中对齐
    .alignItems(HorizontalAlign.End)
    .backgroundColor('#DE2710')
  }
}

103.png

  • Row容器内子元素在垂直方向上的排列

VerticalAlign.Top:子元素在垂直方向顶部对齐。

@Entry
@Component
struct Index{
  build() {
    Row({space:5}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')
    }
    .width('100%')
    .height(200)
    // 垂直方向 顶部对齐
    .alignItems(VerticalAlign.Top)
    .backgroundColor('#DE2710')
  }
}

104.png

VerticalAlign.Center:子元素在垂直方向居中对齐。

@Entry
@Component
struct Index{
  build() {
    Row({space:5}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')
    }
    .width('100%')
    .height(200)
    // 垂直方向 居中对齐
    .alignItems(VerticalAlign.Center)
    .backgroundColor('#DE2710')
  }
}

105.png

VerticalAlign.Bottom:子元素在垂直方向底部对齐

@Entry
@Component
struct Index{
  build() {
    Row({space:5}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')

      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor('#9EC4E7')
    }
    .width('100%')
    .height(200)
    // 垂直方向 底部对齐
    .alignItems(VerticalAlign.Bottom)
    .backgroundColor('#DE2710')
  }
}

106.png

2.4、布局子元素在主轴上的对齐方式

在布局容器内,可以通过justifyContent属性设置子元素在容器主轴上的排列方式。可以从主轴起始位置开始排布,也可以从主轴结束位置开始排布,或者均匀分割主轴的空间。

  • Column容器内子元素在垂直方向上的排列

justifyContent(FlexAlign.Start):元素在垂直方向首端对齐,第一个元素与行首对齐,同时后续的元素与前一个对齐。

@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 元素在垂直方向首端对齐,第一个元素与行首对齐,同时后续的元素与前一个对齐。
    .justifyContent(FlexAlign.Start)
  }
}

201.png

justifyContent(FlexAlign.Center):元素在垂直方向中心对齐,第一个元素与行首的距离与最后一个元素与行尾距离相同

@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 元素在垂直方向中心对齐,第一个元素与行首的距离与最后一个元素与行尾距离相同。
    .justifyContent(FlexAlign.Center)
  }
}

202.png

justifyContent(FlexAlign.End):元素在垂直方向尾部对齐,最后一个元素与行尾对齐,其他元素与后一个对齐。
@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 元素在垂直方向尾部对齐,最后一个元素与行尾对齐,其他元素与后一个对齐
    .justifyContent(FlexAlign.End)
  }
}

203.png

justifyContent(FlexAlign.SpaceBetween):垂直方向均匀分配元素,相邻元素之间距离相同。第一个元素与行首对齐,最后一个元素与行尾对齐。

@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 垂直方向均匀分配元素,相邻元素之间距离相同。
    // 第一个元素与行首对齐,最后一个元素与行尾对齐
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

204.png

justifyContent(FlexAlign.SpaceAround):垂直方向均匀分配元素,相邻元素之间距离相同。第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半。
@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 垂直方向均匀分配元素,相邻元素之间距离相同。
    // 第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半
    .justifyContent(FlexAlign.SpaceAround)
  }
}

205.png

justifyContent(FlexAlign.SpaceEvenly):垂直方向均匀分配元素,相邻元素之间的距离、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样。
@Entry
@Component
struct Index{
  build() {
    Column({}) {
      Column() {
        Text("item1")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('80%')
      .height(50)
      .backgroundColor(0xF5DEB3)
    }
    .width('100%')
    .height(300)
    .backgroundColor('#6E7173')
    // 垂直方向均匀分配元素,
    // 相邻元素之间的距离、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样
    .justifyContent(FlexAlign.SpaceEvenly)
  }
}

206.png

  • Row容器内子元素在水平方向上的排列

justifyContent(FlexAlign.Start):元素在水平方向方向首端对齐,第一个元素与行首对齐,同时后续的元素与前一个对齐

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 元素在水平方向方向首端对齐,
    // 第一个元素与行首对齐,同时后续的元素与前一个对齐
    .justifyContent(FlexAlign.Start)
  }
}

301.png

justifyContent(FlexAlign.Center):元素在水平方向方向中心对齐,第一个元素与行首的距离与最后一个元素与行尾距离相同

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 元素在水平方向方向中心对齐,
    // 第一个元素与行首的距离与最后一个元素与行尾距离相同
    .justifyContent(FlexAlign.Center)
  }
}

101.png

justifyContent(FlexAlign.End):元素在水平方向方向尾部对齐,最后一个元素与行尾对齐,其他元素与后一个对齐

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 元素在水平方向方向尾部对齐,
    // 最后一个元素与行尾对齐,其他元素与后一个对齐
    .justifyContent(FlexAlign.End)
  }
}

102.png

justifyContent(FlexAlign.SpaceBetween):水平方向方向均匀分配元素,相邻元素之间距离相同。第一个元素与行首对齐,最后一个元素与行尾对齐

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 水平方向方向均匀分配元素,相邻元素之间距离相同。
    // 第一个元素与行首对齐,最后一个元素与行尾对齐
    .justifyContent(FlexAlign.SpaceBetween)
  }
}

201.png

justifyContent(FlexAlign.SpaceAround):水平方向方向均匀分配元素,相邻元素之间距离相同。第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 水平方向方向均匀分配元素,相邻元素之间距离相同。
    // 第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半
    .justifyContent(FlexAlign.SpaceAround)
  }
}

202.png

justifyContent(FlexAlign.SpaceEvenly):水平方向方向均匀分配元素,相邻元素之间的距离、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样

@Entry
@Component
struct Index{
  build() {
    Row({}) {
      Column() {
        Text("item1")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)
      Column() {
        Text("item2")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xD2B48C)
      Column() {
        Text("item3")
      }
      .width('25%')
      .height(40)
      .backgroundColor(0xF5DEB3)}
    .width('100%')
    .height(200)
    .backgroundColor('#6E7173')
    // 水平方向方向均匀分配元素,
    // 相邻元素之间的距离、第一个元素与行首的间距、最后一个元素到行尾的间距都完全一样
    .justifyContent(FlexAlign.SpaceEvenly)
  }
}

203.png

## 2.5、自适应拉伸 在线性布局下,常用空白填充组件Blank,在容器主轴方向自动填充空白空间,达到自适应拉伸效果。Row和Column作为容器,只需要添加宽高为百分比,当屏幕宽高发生变化时,会产生自适应效果。 ``` @Entry @Component struct Index { build() { Column() { Row() { Text('Bluetooth') .fontSize(18) Blank() Toggle({ type: ToggleType.Switch, isOn: true }) } .backgroundColor(0xFFFFFF) .borderRadius(15) .padding({ left: 12 }) .width('100%') } .backgroundColor(0xEFEFEF) .padding(20) .width('100%') } } ``` ![1001.gif](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/04c0a64d650f4e78a28a48059a9f5aa2~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5pWF5aeL:q75.awebp?rk3s=f64ab15b&x-expires=1771317660&x-signature=EShzKxWO7yCMBH8qjbtg%2F1Ps50w%3D)

2.6、自适应缩放

自适应缩放是指子组件随容器尺寸的变化而按照预设的比例自动调整尺寸,适应各种不同大小的设备。在线性布局中,可以使用以下两种方法实现自适应缩放。

  • 父容器尺寸确定时,使用layoutWeight属性设置子组件和兄弟元素在主轴上的权重,忽略元素本身尺寸设置,使它们在任意尺寸的设备下自适应占满剩余空间
@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('1:2:3')
        .width('100%')
      Row() {
        Column() {
          Text('layoutWeight(1)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(1)
        .backgroundColor(0xF5DEB3)
        .height('100%')

        Column() {
          Text('layoutWeight(2)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(2)
        .backgroundColor(0xD2B48C)
        .height('100%')

        Column() {
          Text('layoutWeight(3)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(3)
        .backgroundColor(0xF5DEB3)
        .height('100%')
      }
      .backgroundColor(0xffd306)
      .height('30%')

      Text('2:5:3')
        .width('100%')
      Row() {
        Column() {
          Text('layoutWeight(2)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(2)
        .backgroundColor(0xF5DEB3)
        .height('100%')

        Column() {
          Text('layoutWeight(5)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(5)
        .backgroundColor(0xD2B48C)
        .height('100%')

        Column() {
          Text('layoutWeight(3)')
            .textAlign(TextAlign.Center)
        }
        .layoutWeight(3)
        .backgroundColor(0xF5DEB3)
        .height('100%')
      }
      .backgroundColor(0xffd306)
      .height('30%')
    }
  }
}

301.png

2.7、自适应延伸

自适应延伸是指在不同尺寸设备下,当页面的内容超出屏幕大小而无法完全显示时,可以通过滚动条进行拖动展示。这种方法适用于线性布局中内容无法一屏展示的场景。通常有以下两种实现方式。

  • 在List中添加滚动条:当List子项过多一屏放不下时,可以将每一项子元素放置在不同的组件中,通过滚动条进行拖动展示。可以通过scrollBar属性设置滚动条的常驻状态,edgeEffect属性设置拖动到内容最末端的回弹效果。

  • 使用Scroll组件:在线性布局中,开发者可以进行垂直方向或者水平方向的布局。当一屏无法完全显示时,可以在Column或Row组件的外层包裹一个可滚动的容器组件Scroll来实现可滑动的线性布局。

总结

这篇比较长,感谢翻阅。

Power!

小猫力量.png