码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • HarmonyOS鸿蒙学习笔记(11)Flex组件alignItems属性说明


    Flex组件alignItems属性

    • 1、代码示例
    • 2、FlexDirection.Row情况下各个ItemAlign的运行情况。
      • 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的运行效果:
      • 2.6、alignItems:ItemAlign.Stretch的运行效果:
    • 3、FlexDirection.Column情况下各个ItemAlign的运行情况。
      • 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的运行效果:

    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还有两种类型RowReverse和ColumnReverse:其中RowReverse跟Row方向相反;ColumnReverse与Column方向相反,在这里就不在赘述,读者可以执行验证之。
    在这里插入图片描述
    关于Flex的更多情况说明,可以参考鸿蒙官方文档。

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

  • 相关阅读:
    【转】数据库索引详细介绍
    简化javabean开发-->Lombok
    javascript--类型检测 type of 和 instanceof
    kafka-listener
    C 语言网络编程 — NAT 网关运行原理
    Linux环境下 安装部署mysql
    使用百度飞桨EasyDL实现AI文章自动分类
    「Redis数据结构」字符串对象String
    44、DeVRF
    MYSQL(索引篇)
  • 原文地址:https://blog.csdn.net/chunqiuwei/article/details/126941099
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号