当前位置:   article > 正文

鸿蒙HarmonyOS实战-ArkUI组件(Row/Column)_harmonyos容器自适应高度

harmonyos容器自适应高度

前言

HarmonyOS的布局组件是一组用于构建用户界面布局的组件,包括线性布局、相对布局、网格布局等。这些组件帮助开发者以简单和灵活的方式管理和组织应用程序中的视图,并支持多种不同的设备屏幕尺寸和方向。使用HarmonyOS的布局组件可以提高应用程序的可读性和可维护性,并帮助快速构建适应不同设备的用户界面。

常见页面结构图:
在这里插入图片描述
不就元素组成:
在这里插入图片描述

一、Row/Column

1.线性布局

线性布局(LinearLayout)是一种常用的UI布局方式,通过线性容器 Row 和 Column 构建。线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column 容器内子元素按照垂直方向排列,Row 容器内子元素按照水平方向排列。根据不同的排列方向,开发者可选择使用 Row 或 Column 容器创建线性布局。线性布局的优点是可以根据不同的排列需求创建灵活的布局,同时也方便管理子元素的位置和大小。

容器组件排列方向特点
Column垂直子元素按垂直方向排列
Row水平子元素按水平方向排列

Column容器内子元素排列示意图:
在这里插入图片描述
Row容器内子元素排列示意图:
在这里插入图片描述

2.间距

  1. Column({ space: 20 })
  2. Row({ space: 35 })

在这里插入图片描述
在这里插入图片描述

3.对齐方式

3.1 水平对齐

Column({}) {}.alignItems(HorizontalAlign.Start)

在这里插入图片描述

3.2 垂直对齐

Column({}) {}.alignItems(VerticalAlign.Top)

在这里插入图片描述

4.排列方式

4.1 水平排列

Column({}) {}.justifyContent(FlexAlign.Start)

在这里插入图片描述

4.2 垂直排列

Row({}) {}.justifyContent(FlexAlign.Start)

在这里插入图片描述

5.自适应拉伸

5.1 水平拉伸

因为自适应一般是讲宽度,其实高度也行,但原理一样

Column({}) {}.width('100%')

在这里插入图片描述

6.自适应缩放

6.1 权重

  1. @Entry
  2. @Component
  3. struct layoutWeightExample {
  4. build() {
  5. Column() {
  6. Text('1:2:3').width('100%')
  7. Row() {
  8. Column() {
  9. Text('layoutWeight(1)')
  10. .textAlign(TextAlign.Center)
  11. }.layoutWeight(1).backgroundColor(0xF5DEB3).height('100%')
  12. Column() {
  13. Text('layoutWeight(2)')
  14. .textAlign(TextAlign.Center)
  15. }.layoutWeight(2).backgroundColor(0xD2B48C).height('100%')
  16. Column() {
  17. Text('layoutWeight(3)')
  18. .textAlign(TextAlign.Center)
  19. }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%')
  20. }.backgroundColor(0xffd306).height('30%')
  21. Text('2:5:3').width('100%')
  22. Row() {
  23. Column() {
  24. Text('layoutWeight(2)')
  25. .textAlign(TextAlign.Center)
  26. }.layoutWeight(2).backgroundColor(0xF5DEB3).height('100%')
  27. Column() {
  28. Text('layoutWeight(5)')
  29. .textAlign(TextAlign.Center)
  30. }.layoutWeight(5).backgroundColor(0xD2B48C).height('100%')
  31. Column() {
  32. Text('layoutWeight(3)')
  33. .textAlign(TextAlign.Center)
  34. }.layoutWeight(3).backgroundColor(0xF5DEB3).height('100%')
  35. }.backgroundColor(0xffd306).height('30%')
  36. }
  37. }
  38. }

在这里插入图片描述

6.2 百分比

  1. @Entry
  2. @Component
  3. struct WidthExample {
  4. build() {
  5. Column() {
  6. Row() {
  7. Column() {
  8. Text('left width 20%')
  9. .textAlign(TextAlign.Center)
  10. }.width('20%').backgroundColor(0xF5DEB3).height('100%')
  11. Column() {
  12. Text('center width 50%')
  13. .textAlign(TextAlign.Center)
  14. }.width('50%').backgroundColor(0xD2B48C).height('100%')
  15. Column() {
  16. Text('right width 30%')
  17. .textAlign(TextAlign.Center)
  18. }.width('30%').backgroundColor(0xF5DEB3).height('100%')
  19. }.backgroundColor(0xffd306).height('30%')
  20. }
  21. }
  22. }

在这里插入图片描述

7.Scroll组件自适应延伸

7.1 列自适应延伸

  1. @Entry
  2. @Component
  3. struct ScrollExample {
  4. scroller: Scroller = new Scroller();
  5. private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  6. build() {
  7. Scroll(this.scroller) {
  8. Column() {
  9. ForEach(this.arr, (item) => {
  10. Text(item.toString())
  11. .width('90%')
  12. .height(150)
  13. .backgroundColor(0xFFFFFF)
  14. .borderRadius(15)
  15. .fontSize(16)
  16. .textAlign(TextAlign.Center)
  17. .margin({ top: 10 })
  18. }, item => item)
  19. }.width('100%')
  20. }
  21. .backgroundColor(0xDCDCDC)
  22. .scrollable(ScrollDirection.Vertical) // 滚动方向纵向
  23. .scrollBar(BarState.On) // 滚动条常驻显示
  24. .scrollBarColor(Color.Gray) // 滚动条颜色
  25. .scrollBarWidth(10) // 滚动条宽度
  26. .edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹
  27. }
  28. }

在这里插入图片描述

7.2 行自适应延伸

  1. @Entry
  2. @Component
  3. struct ScrollExample {
  4. scroller: Scroller = new Scroller();
  5. private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  6. build() {
  7. Scroll(this.scroller) {
  8. Row() {
  9. ForEach(this.arr, (item) => {
  10. Text(item.toString())
  11. .height('90%')
  12. .width(150)
  13. .backgroundColor(0xFFFFFF)
  14. .borderRadius(15)
  15. .fontSize(16)
  16. .textAlign(TextAlign.Center)
  17. .margin({ left: 10 })
  18. })
  19. }.height('100%')
  20. }
  21. .backgroundColor(0xDCDCDC)
  22. .scrollable(ScrollDirection.Horizontal) // 滚动方向横向
  23. .scrollBar(BarState.On) // 滚动条常驻显示
  24. .scrollBarColor(Color.Gray) // 滚动条颜色
  25. .scrollBarWidth(10) // 滚动条宽度
  26. .edgeEffect(EdgeEffect.Spring) // 滚动到边沿后回弹
  27. }
  28. }

在这里插入图片描述

二、登录案例

  1. build() {
  2. Column({space:20}) {
  3. Image( 'logo .png')
  4. TextInput({placeholder:'用户名'})
  5. TextInput({placeholder:'密码'})
  6. .type(InputType.Password)
  7. .showPasswordIcon(true)
  8. Button('登录')
  9. Row(){
  10. Checkbox()
  11. Text('记住我')
  12. .fontColor('#36D')
  13. }
  14. }
  15. .height('100%')
  16. }

执行效果:
在这里插入图片描述

 声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】

推荐阅读
相关标签