首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

鸿蒙HarmonyOS实战-ArkUI组件(Row/Column)

  • 25-02-22 03:21
  • 3908
  • 9614
blog.csdn.net

前言

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. }

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

 ?写在最后

  • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing?,不定期分享原创知识。
  • 想要获取文中提到的学习资料,请点击→全套鸿蒙HarmonyOS学习资料
  • 或者关注小编后私信回复【666】也可获取资料哦~~

注:本文转载自blog.csdn.net的蜀道山QAQ的文章"https://blog.csdn.net/shudaoshanQAQ/article/details/135395225"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top