Harmony之学习Column&Row组件的使用

1 概述

一个丰富的页面需要很多组件组成,那么,我们如何才能让这些组件有条不紊地在页面上布局呢?这就需要借助容器组件来实现。

容器组件是一种比较特殊的组件,它可以包含其他的组件,而且按照一定的规律布局,帮助开发者生成精美的页面。容器组件除了放置基础组件外,也可以放置容器组件,通过多层布局的嵌套,可以布局出更丰富的页面。

ArkTS为我们提供了丰富的容器组件来布局页面,本文将以构建登录页面为例,介绍Column和Row组件的属性与使用。
在这里插入图片描述

2 组件介绍

布局容器概念

线性布局容器表示按照垂直方向或者水平方向排列子组件的容器,ArkTS提供了Column和Row容器来实现线性布局。

  • Column表示沿垂直方向布局的容器。
  • Row表示沿水平方向布局的容器。

主轴和交叉轴概念

在布局容器中,默认存在两根轴,分别是主轴和交叉轴,这两个轴始终是相互垂直的。不同的容器中主轴的方向不一样的。

  1. 主轴:在Column容器中的子组件是按照从上到下的垂直方向布局的,其主轴的方向是垂直方向;在Row容器中的组件是按照从左到右的水平方向布局的,其主轴的方向是水平方向。
    在这里插入图片描述
  2. 交叉轴:与主轴垂直相交的轴线,如果主轴是垂直方向,则交叉轴就是水平方向;如果主轴是水平方向,则交叉轴是垂直方向。
    在这里插入图片描述

属性介绍

了解布局容器的主轴和交叉轴,主要是为了让大家更好地理解子组件在主轴和交叉轴的排列方式。

接下来,我们将详细讲解Column和Row容器的两个属性justifyContent和alignItems。
|

属性名称描述
justifyContent设置子组件在主轴方向上的对齐格式。
alignItems设置子组件在交叉轴方向上的对齐格式。

1. 主轴方向的对齐(justifyContent)

子组件在主轴方向上的对齐使用justifyContent属性来设置,其参数类型是FlexAlign。FlexAlign定义了以下几种类型:

  • Start:元素在主轴方向首端对齐,第一个元素与行首对齐,同时后续的元素与前一个对齐。(默认)
    Column容器
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.Start)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.Start)
      .width('100%')
      // .height('100%')
  }
}

在这里插入图片描述

  • Center:元素在主轴方向中心对齐,第一个元素与行首的距离以及最后一个元素与行尾距离相同。
    Column容器
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述 Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • End:元素在主轴方向尾部对齐,最后一个元素与行尾对齐,其他元素与后一个对齐。
    Column容器
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.End)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.End)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • SpaceBetween:元素在主轴方向均匀分配弹性元素,相邻元素之间距离相同。 第一个元素与行首对齐,最后一个元素与行尾对齐。
    Column容器
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • SpaceAround:元素在主轴方向均匀分配弹性元素,相邻元素之间距离相同。 第一个元素到行首的距离和最后一个元素到行尾的距离是相邻元素之间距离的一半。
    Column
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceAround)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceAround)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • SpaceEvenly:元素在主轴方向等间距布局,无论是相邻元素还是边界元素到容器的间距都一样。
    Column容器
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceEvenly)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
Row容器

@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .justifyContent(FlexAlign.SpaceEvenly)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

2.交叉轴方向的对齐(alignItems)

子组件在交叉轴方向上的对齐方式使用alignItems属性来设置。

Column容器的主轴是垂直方向,交叉轴是水平方向,其参数类型为HorizontalAlign(水平对齐),HorizontalAlign定义了以下几种类型:

  • Start:设置子组件在水平方向上按照起始端对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(HorizontalAlign.Start)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • Center(默认值):设置子组件在水平方向上居中对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(HorizontalAlign.Center)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • End:设置子组件在水平方向上按照末端对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(HorizontalAlign.End)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
Row容器的主轴是水平方向,交叉轴是垂直方向,其参数类型为VerticalAlign(垂直对齐),VerticalAlign定义了以下几种类型:

  • Top:设置子组件在垂直方向上居顶部对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(VerticalAlign.Top)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • Center(默认值):设置子组件在竖直方向上居中对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(VerticalAlign.Center)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述

  • Bottom:设置子组件在竖直方向上居底部对齐。
@Entry
@Component
struct Index {
  @State message: string = 'Hi'

  build() {
      Row() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .alignItems(VerticalAlign.Bottom)
      .width('100%')
      .height('100%')
  }
}

在这里插入图片描述
接口介绍
接下来,我们介绍Column和Row容器的接口。

容器组件接口
ColumnColumn(value?:{space?: string number})
RowRow(value?:{space?: string number})

Column和Row容器的接口都有一个可选参数space,表示子组件在主轴方向上的间距。

效果如下:
在这里插入图片描述

3 参考链接

Column组件的相关API参考:Column组件
Row组件的相关API参考:Row组件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值