HarmonyOS ArkTS组件 | Flex 以弹性方式布局子组件的容器组件 学习记录

news/2024/10/9 0:51:28

HarmonyOS ArkTS组件 | Flex 以弹性方式布局子组件的容器组件 学习记录

前言:最近需要用到弹性布局,记录一下。(忽略图片水印QAQ)

说明:

  1. Flex组件在渲染时存在二次布局过程,因此在对性能有严格要求的场景下建议使用Column、Row代替。
  2. Flex组件主轴默认不设置时撑满父容器,Column、Row组件主轴不设置时默认是跟随子节点大小。

接口:

Flex(value?: { direction?: FlexDirection, wrap?: FlexWrap, justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })

参数:

direction:
子组件在Flex容器上排列的方向,即主轴的方向。

// xxx.ets
@Entry
@Component
struct FlexExample1 {build() {Column() {Column({ space: 5 }) {Text('direction:Row').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ direction: FlexDirection.Row }) {Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)Text('2').width('20%').height(50).backgroundColor(0xD2B48C)Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)Text('4').width('20%').height(50).backgroundColor(0xD2B48C)}.height(70).width('90%').padding(10).backgroundColor(0xAFEEEE)Text('direction:RowReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ direction: FlexDirection.RowReverse }) {Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)Text('2').width('20%').height(50).backgroundColor(0xD2B48C)Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)Text('4').width('20%').height(50).backgroundColor(0xD2B48C)}.height(70).width('90%').padding(10).backgroundColor(0xAFEEEE)Text('direction:Column').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ direction: FlexDirection.Column }) {Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)Text('2').width('100%').height(40).backgroundColor(0xD2B48C)Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)Text('4').width('100%').height(40).backgroundColor(0xD2B48C)}.height(160).width('90%').padding(10).backgroundColor(0xAFEEEE)Text('direction:ColumnReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ direction: FlexDirection.ColumnReverse }) {Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)Text('2').width('100%').height(40).backgroundColor(0xD2B48C)Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)Text('4').width('100%').height(40).backgroundColor(0xD2B48C)}.height(160).width('90%').padding(10).backgroundColor(0xAFEEEE)}.width('100%').margin({ top: 5 })}.width('100%')}
}

运行结果:

wrap:
Flex容器是单行/列还是多行/列排列。
说明:
在多行布局时,通过交叉轴方向,确认新行堆叠方向。

// xxx.ets
@Entry
@Component
struct FlexExample2 {build() {Column() {Column({ space: 5 }) {Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ wrap: FlexWrap.Wrap }) {Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)Text('2').width('50%').height(50).backgroundColor(0xD2B48C)Text('3').width('50%').height(50).backgroundColor(0xD2B48C)}.width('90%').padding(10).backgroundColor(0xAFEEEE)Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ wrap: FlexWrap.NoWrap }) {Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)Text('2').width('50%').height(50).backgroundColor(0xD2B48C)Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)}.width('90%').padding(10).backgroundColor(0xAFEEEE)Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')Flex({ wrap: FlexWrap.WrapReverse , direction:FlexDirection.Row }) {Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)Text('2').width('50%').height(50).backgroundColor(0xD2B48C)Text('3').width('50%').height(50).backgroundColor(0xD2B48C)}.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)}.width('100%').margin({ top: 5 })}.width('100%')}
}

运行结果:

justifyContent:
子组件在Flex容器主轴上的对齐格式。

// xxx.ets
@Component
struct JustifyContentFlex {justifyContent : numberbuild() {Flex({ justifyContent: this.justifyContent }) {Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)Text('2').width('20%').height(50).backgroundColor(0xD2B48C)Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)}.width('90%').padding(10).backgroundColor(0xAFEEEE)}
}@Entry
@Component
struct FlexExample3 {build() {Column() {Column({ space: 5 }) {Text('justifyContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.Start })Text('justifyContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.Center })Text('justifyContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.End })Text('justifyContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.SpaceBetween })Text('justifyContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.SpaceAround })Text('justifyContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')JustifyContentFlex({ justifyContent: FlexAlign.SpaceEvenly })}.width('100%').margin({ top: 5 })}.width('100%')}
}

运行结果:

alignItems:
子组件在Flex容器交叉轴上的对齐格式。

// xxx.ets
@Component
struct AlignItemsFlex {alignItems : numberbuild() {Flex({ alignItems: this.alignItems }) {Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)Text('2').width('33%').height(40).backgroundColor(0xD2B48C)Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)}.size({width: '90%', height: 80}).padding(10).backgroundColor(0xAFEEEE)}
}@Entry
@Component
struct FlexExample4 {build() {Column() {Column({ space: 5 }) {Text('alignItems:Auto').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.Auto })Text('alignItems:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.Start })Text('alignItems:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.Center })Text('alignItems:End').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.End })Text('alignItems:Stretch').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.Stretch })Text('alignItems:Baseline').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignItemsFlex({ alignItems: ItemAlign.Baseline })}.width('100%').margin({ top: 5 })}.width('100%')}
}

运行结果:

alignContent:
交叉轴中有额外的空间时,多行内容的对齐方式。仅在wrap为Wrap或WrapReverse下生效。

// xxx.ets
@Component
struct AlignContentFlex {alignContent: numberbuild() {Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) {Text('1').width('50%').height(20).backgroundColor(0xF5DEB3)Text('2').width('50%').height(20).backgroundColor(0xD2B48C)Text('3').width('50%').height(20).backgroundColor(0xD2B48C)}.size({ width: '90%', height: 90 }).padding(10).backgroundColor(0xAFEEEE)}
}@Entry
@Component
struct FlexExample5 {build() {Column() {Column({ space: 5 }) {Text('alignContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.Start })Text('alignContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.Center })Text('alignContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.End })Text('alignContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.SpaceBetween })Text('alignContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.SpaceAround })Text('alignContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')AlignContentFlex({ alignContent: FlexAlign.SpaceEvenly })}.width('100%').margin({ top: 5 })}.width('100%')}
}

运行结果:

———————————— 封装线 ——————————————————

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/42481.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

2024计算机组成原理复习——第一章

计算机组成原理复习——第一章一、计算机系统概括 本笔记不用于商业用途,内容参考《2025年计算机组成原理——考研复习指导》以及其对应的b站免费视频课(图文信息主要来自于此) (一)计算机系统结构层次 1. 计算机系统的基本组成硬件:有形的物理设备,计算机系统中实际物理…

[转帖]Linux Kernel 6.6 确认成为 LTS 版本

https://www.kernel.org/category/releases.html Greg Kroah-Hartman 已经宣布 Linux Kernel 6.6 版本为长期支持 (LTS) 版本;支持期限到 2026 年 12 月。 Linux Kernel 6.6 于 10 月 29 日正式发布,是一次包含了新功能、硬件支持、安全增强和性能改进的重大更新。具体包括有…

第4-6次大作业BLOG

目录前言第四次大作业blog设计与分析踩坑心得改进建议总结第五次大作业设计与分析踩坑心得改进建议总结第六次大作业设计与分析踩坑心得改进建议 前言 第四次作业考察了我们list和map的使用,以及对正则表达式的掌握情况,类的设计与方法的使用,第五次作业和第六次作业是电路模…

第二阶段PTA总结

前言 本阶段面向对象程序设计又进行了三次PTA训练,第一次(总第四次)还是接续上轮训练的试卷判分系统的程序设计,后两次都是新的电路设计训练题目。相对来说,第二次电路设计的第一次练习最为简单,第一次和第三次相对复杂。在这个阶段,我已经适应了PTA训练的题量和训练模式…

python-数据分析-Numpy-2

数组对象的方法应用 # -*- coding: utf-8 -*- #数组对象的方法 import matplotlib.pyplot as plt import numpy# 1、 获取描述统计信息 array1 = numpy.random.randint(1, 100, 10) print(array1) #随机数组 [64 84 10 52 3 66 4 31 79 7]#计算总和、平均值、中位数 pri…

数据结构之跳表

原始论文: https://15721.courses.cs.cmu.edu/spring2018/papers/08-oltpindexes1/pugh-skiplists-cacm1990.pdf 基本原理 跳表的查找与插入:代码实现 未完成,待续。。。。

PTA训练集阶段总结blog

这次是4-6次的pta作业的一个总结性blog目录PTA训练集总结blog1.前言2.设计与分析题目集一 7.4 答题判题程序四关于设计要求:UML类图及设计分析:部分源码:复杂度分析:题目集五 7.1 家具强电电路模拟系统—1关于设计要求:UML类图及设计分析:部分源码:复杂度分析:题目集六…

Redis-6-三种缓存读写策略

2.1 旁路缓存Cache Aside Pattern(旁路缓存)适合读请求比较多的场景Cache Aside Pattern 中服务端需要同时维系 db 和 cache,并且是以 db 的结果为准。 2.1.1 写先更新db 直接删除缓存2.1.2 读先读缓存有,则从缓存返回。 没有,从db中读取返回。再将读取的数据写入缓存2.1.…