• 探索arkui(1)--- 布局(线性/层叠/弹性)


    前端开发布局是指前端开发人员宣布他们开发的新网站或应用程序正式上线的活动。在前端开发布局中,开发人员通常会展示新网站或应用程序的设计、功能和用户体验,并向公众宣传新产品的特点和优势。前端开发布局通常是前端开发领域的重要事件,吸引了广泛的关注和关注。通过前端开发布局,开发人员可以推广他们的新产品,增加用户的关注和使用,并提高自己的品牌知名度。同时,前端开发布局也为用户提供了了解和体验新产品的机会,帮助他们更好地了解和使用新的网站或应用程序。鸿蒙开发亦是如此。

    三种布局的介绍 

    线性

    官网描述

    线性布局(LinearLayout)是开发中最常用的布局,通过线性容器Row和Column构建。线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column容器内子元素按照垂直方向排列,Row容器内子元素按照水平方向排列。根据不同的排列方向,开发者可选择使用Row或Column容器创建线性布局。

    我的理解

    就是按照顺序和逻辑,一个一个渲染

    代码

    1. Row(){
    2. Button('单单省钱').backgroundColor(Color.Orange).margin({left:10})
    3. Text('首页').fontColor(Color.White).fontWeight(FontWeight.Bold).margin({left:30})
    4. Text('小时达').fontColor(Color.White).margin({left:30})
    5. }.height(70).width('100%').backgroundColor(Color.Red)

    效果

    层叠

    官方描述

    层叠布局(StackLayout)用于在屏幕上预留一块区域来显示组件中的元素,提供元素可以重叠的布局。层叠布局通过Stack容器组件实现位置的固定定位与层叠,容器中的子元素(子组件)依次入栈,后一个子元素覆盖前一个子元素,子元素可以叠加,也可以设置位置。

    容器内的子元素(子组件)的顺序为Item1->Item2->Item3。

    我的理解 

    叠罗汉,卸载后面的默认在上面

    代码

    1. Stack({alignContent: Alignment.Center}){
    2. Stack({alignContent: Alignment.End}){
    3. Text().width('90%').backgroundColor(Color.White).borderRadius(25).height(40)
    4. Text('千兆拓展坞').width('50%').margin({right:75})
    5. Button('搜索').width('15%').height('70%').backgroundColor(Color.Red).margin({right:2})
    6. }
    7. }.height(50).width('100%').backgroundColor(Color.Red)

    效果

    弹性

    官方描述

    弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。容器默认存在主轴与交叉轴,子元素默认沿主轴排列,子元素在主轴方向的尺寸称为主轴尺寸,在交叉轴方向的尺寸称为交叉轴尺寸。弹性布局在开发场景中用例特别多,比如页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等等。

    我的理解 

    最好用来渲染容器组件,每一个都一模一样

    代码

    1. Row(){
    2. Stack({ alignContent: Alignment.Bottom }){
    3. Stack({ alignContent: Alignment.Bottom }) {
    4. Flex({ wrap: FlexWrap.Wrap }) {
    5. ForEach(this.arr, (item) => {
    6. Text(item)
    7. .width("15%")
    8. .height(100)
    9. .fontSize(16)
    10. .margin(5)
    11. .textAlign(TextAlign.Center)
    12. .borderRadius(10)
    13. .backgroundColor(0xFFFFFF)
    14. .borderColor(Color.Black)
    15. }, item => item)
    16. }
    17. }.width('90%').backgroundColor(Color.White)
    18. }.backgroundColor(Color.White).borderColor(Color.Black)
    19. }.backgroundColor(Color.Red)

    效果

    最终代码

    1. @Entry
    2. @Component
    3. struct Index {
    4. private swiperController: SwiperController = new SwiperController()
    5. private arr: string[]=['领红包','汪汪赚钱','签到体现','1分抽奖','充值中心']
    6. build() {
    7. Column(){
    8. Row(){
    9. Button('单单省钱').backgroundColor(Color.Orange).margin({left:10})
    10. Text('首页').fontColor(Color.White).fontWeight(FontWeight.Bold).margin({left:30})
    11. Text('小时达').fontColor(Color.White).margin({left:30})
    12. }.height(70).width('100%').backgroundColor(Color.Red)
    13. Stack({alignContent: Alignment.Center}){
    14. Stack({alignContent: Alignment.End}){
    15. Text().width('90%').backgroundColor(Color.White).borderRadius(25).height(40)
    16. Text('千兆拓展坞').width('50%').margin({right:75})
    17. Button('搜索').width('15%').height('70%').backgroundColor(Color.Red).margin({right:2})
    18. }
    19. }.height(50).width('100%').backgroundColor(Color.Red)
    20. Row(){
    21. Stack({ alignContent: Alignment.Bottom }){
    22. Stack({ alignContent: Alignment.Bottom }) {
    23. Flex({ wrap: FlexWrap.Wrap }) {
    24. ForEach(this.arr, (item) => {
    25. Text(item)
    26. .width("15%")
    27. .height(100)
    28. .fontSize(16)
    29. .margin(5)
    30. .textAlign(TextAlign.Center)
    31. .borderRadius(10)
    32. .backgroundColor(0xFFFFFF)
    33. .borderColor(Color.Black)
    34. }, item => item)
    35. }
    36. }.width('90%').backgroundColor(Color.White)
    37. }.backgroundColor(Color.White).borderColor(Color.Black)
    38. }.backgroundColor(Color.Red)
    39. }
    40. }
    41. }

    最终效果

    可能涉及到的枚举类

    位置相关Alignment

  • 相关阅读:
    Part2_扩展MATSIM_Subpart6_附加选择维度_第27章 目的地创新
    传统用户管理方案有哪些利弊?
    无监督学习KMeans学习笔记和实例
    移动端开发——APP端上H5容器化建设
    sql根据出生日期计算当前年龄 函数TIMESTAMPDIFF()
    正则表达式入门教程
    简单理解MySQL的存储引擎
    基于nodejs+vue语言的酒店管理系统
    【云原生持续交付和自动化测试】5.3 持续交付和DevOps实践基础知识
    【Linux学习】—Linux常用指令(一)
  • 原文地址:https://blog.csdn.net/weixin_44808225/article/details/134425108