HarmonyOS鸿蒙学习笔记(11)Flex组件alignItems属性说明

本文通过代码示例详细解析了Flex布局中alignItems属性的作用,并展示了在不同FlexDirection设置下各ItemAlign值的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、代码示例

Flex弹性布局组件。本文通过一个例子代码来验证Flex的alignItems属性。

@Entry
@Component
struct Index {
  build() {

    Column() {
      Flex({ alignItems:ItemAlign.Stretch,direction:FlexDirection.Row}) {
        Text('1').textAlign(TextAlign.Center).width('20%').height(50).backgroundColor(0xF5DEB3)
        Text('2').textAlign(TextAlign.Center).width('20%').height(100).backgroundColor(0xD2B48C)
        Text('3').textAlign(TextAlign.Center).width('20%').height(150).backgroundColor(0xF5DEB3)
        Text('4').textAlign(TextAlign.Center).width('20%').height(200).backgroundColor(0xD2B48C)
        Text('5').textAlign(TextAlign.Center).width('20%').height(250).backgroundColor(0xF5DEB3)
      }
      .height(300)
      .width('100%')
      .backgroundColor(0xAFEEEE)
    }.justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .height('100%')
    .width('100%')
  }
}

如上面不如所示,Flex里面有五个宽度一样高度不同的的Text文本控件.ItemAlign属性控制着子组件在Flex容器交叉轴上的对齐格式,ItemAlign的值类型如下
在这里插入图片描述

2、FlexDirection.Row情况下各个ItemAlign的运行情况。

默认情况下Flex的direction属性值为FlexDirection.Row

2.1、alignItems:ItemAlign.Auto的运行效果

在这里插入图片描述

2.2、alignItems:ItemAlign.Baseline的运行效果:

这个暂时没有看明白布局原理,后面再验证之。
在这里插入图片描述

2.3、alignItems:ItemAlign.Center的运行效果:

在这里插入图片描述

2.4、alignItems:ItemAlign.End的运行效果:

在这里插入图片描述

2.5、alignItems:ItemAlign.Start的运行效果:

这个跟Auto运行效果一样。
在这里插入图片描述

2.6、alignItems:ItemAlign.Stretch的运行效果:

这个属性有点迷惑,其描述是在未设置尺寸时拉伸到容器尺寸,但是我代码中我明明设置了尺寸。疑惑之
在这里插入图片描述

3、FlexDirection.Column情况下各个ItemAlign的运行情况。

在这里为了方便说明,我们将代码改动下,将Text的高度设置一样,宽度设置不同,代码改动如下:


    Column() {
      Flex({ alignItems:ItemAlign.Stretch,direction:FlexDirection.Column}) {
        Text('1').textAlign(TextAlign.Center).width(50).height('20%').backgroundColor(0xF5DEB3)
        Text('2').textAlign(TextAlign.Center).width(100).height('20%').backgroundColor(0xD2B48C)
        Text('3').textAlign(TextAlign.Center).width(150).height('20%').backgroundColor(0xF5DEB3)
        Text('4').textAlign(TextAlign.Center).width(200).height('20%').backgroundColor(0xD2B48C)
        Text('5').textAlign(TextAlign.Center).width(250).height('20%').backgroundColor(0xF5DEB3)
      }
      .height(300)
      .width('100%')
      .backgroundColor(0xAFEEEE)
    }.justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .height('100%')
    .width('100%')

3.1、alignItems:ItemAlign.Stretch的运行效果:

在这里插入图片描述

3.2、alignItems:ItemAlign.Start的运行效果:

在这里插入图片描述

3.3、alignItems:ItemAlign.End的运行效果:

在这里插入图片描述

3.4、alignItems:ItemAlign.Center的运行效果:

在这里插入图片描述

3.5、alignItems:ItemAlign.Auto的运行效果:

在这里插入图片描述

另外FlexDirection还有两种类型RowReverseColumnReverse:其中RowReverseRow方向相反;ColumnReverseColumn方向相反,在这里就不在赘述,读者可以执行验证之。
在这里插入图片描述
关于Flex的更多情况说明,可以参考鸿蒙官方文档。

参考资料:
Flex官方文档
FlexDirdection官方文档
ItemAlign属性说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭梧悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值