HarmonyOS ArkUI容器类组件-层叠布局容器(Stack)

105 篇文章 2 订阅
11 篇文章 0 订阅

ArkUI开发框架提了堆叠容器组件 Stack ,它的布局方式是把子组件按照设置的对齐方式顺序依次堆叠,后一个子组件覆盖在前一个子组件上边。

Stack定义介绍

interface StackInterface {
  (value?: { alignContent?: Alignment }): StackAttribute;
}
  • alignContent:设置子组件的对其方式, Alignment 定义了以下 9 种对齐方式:

    • TopStart:子组件在 Stack 内靠左上角对齐,简单样例如下所示:
        Stack({alignContent: Alignment.TopStart}) {
          Text('Text1')
            .width(200)
            .height(180)
            .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • Top:设置子组件在 Stack 内靠顶部水平居中对齐,简单样例如下所示:
        Stack({alignContent: Alignment.Top}) {
          Text('Text1')
            .width(200)
            .height(180)
            .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • TopEnd:设置子组件在 Stack 内部靠右上角对齐,简单样例如下所示:
       Stack({alignContent: Alignment.TopEnd}) {
         Text('Text1')
           .width(200)
           .height(180)
           .textAlign(TextAlign.Start)
           .backgroundColor("#aabbcc")

         Text('Text2')
           .width(130)
           .height(100)
           .textAlign(TextAlign.Start)
           .backgroundColor('#bbccaa')

         Text('Text3')       // 被遮挡住了
           .backgroundColor('#ccaabb')

         Text('Text4')
           .width(60)
           .height(45)
           .textAlign(TextAlign.Start)
           .backgroundColor('#abcabc')
       }
       .backgroundColor(Color.Pink)
       .width("100%")
       .height('200')

样例运行结果如下图所示:

  • Start:子组件靠 Stack 左边侧竖直居中对齐,简单样例如下所示:
        Stack({alignContent: Alignment.Start}) {
          Text('Text1')
            .width(200)
            .height(180)
            .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • Center(默认值):设置子组件居中对齐,简单样例如下所示:
        Stack({alignContent: Alignment.Center}) {
          Text('Text1')
            .width(200)
            .height(180)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • End:设置子组件靠右竖直居中对齐,简单样例如下所示:
        Stack({alignContent: Alignment.End}) {
          Text('Text1')
            .width(200)
            .height(180)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • BottomStart:设置子组件左下角对齐,简单样例如下所示:
        Stack({alignContent: Alignment.BottomStart}) {
          Text('Text1')
            .width(200)
            .height(180)
            .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • Bottom:设置子组件底部水平居中对齐,简单样例如下所示:
        Stack({alignContent: Alignment.Bottom}) {
          Text('Text1')
            .width(200)
            .height(180)
            // .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            // .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            // .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

  • BottomEnd:设置子组件右下角对齐,简单样例如下所示:
        Stack({alignContent: Alignment.BottomEnd}) {
          Text('Text1')
            .width(200)
            .height(180)
            // .textAlign(TextAlign.End)
            .backgroundColor("#aabbcc")

          Text('Text2')
            .width(130)
            .height(100)
            // .textAlign(TextAlign.End)
            .backgroundColor('#bbccaa')

          Text('Text3')       // 被遮挡住了
            .backgroundColor('#ccaabb')

          Text('Text4')
            .width(60)
            .height(45)
            // .textAlign(TextAlign.End)
            .backgroundColor('#abcabc')
        }
        .backgroundColor(Color.Pink)
        .width("100%")
        .height('200')

样例运行结果如下图所示:

📢:根据以上示例可知: Stack 组件层叠式布局,尺寸较小的布局会有被遮挡的风险,读者在布局组件的时候需要注意一下。

Stack属性介绍

declare class StackAttribute extends CommonMethod<StackAttribute> {
  alignContent(value: Alignment): StackAttribute;
}
  • alignContent:设置子组件的对齐方式, Alignment 的讲解同上,这里就不再介绍了。

小结

本节简单介绍了 Stack 布局特性:堆叠式,它的使用很简单,唯一需要读者注意的是小的子组件可能出现会被遮挡的情况,熟练该容器组件后就可以构建相对比较复杂的UI了。

码牛课堂也为了积极培养鸿蒙生态人才,让大家都能学习到鸿蒙开发最新的技术,针对一些在职人员、0基础小白、应届生/计算机专业、鸿蒙爱好者等人群,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线。大家可以进行参考学习:https://qr21.cn/FV7h05

①全方位,更合理的学习路径
路线图包括ArkTS基础语法、鸿蒙应用APP开发、鸿蒙能力集APP开发、次开发多端部署开发、物联网开发等九大模块,六大实战项目贯穿始终,由浅入深,层层递进,深入理解鸿蒙开发原理!

②多层次,更多的鸿蒙原生应用
路线图将包含完全基于鸿蒙内核开发的应用,比如一次开发多端部署、自由流转、元服务、端云一体化等,多方位的学习内容让学生能够高效掌握鸿蒙开发,少走弯路,真正理解并应用鸿蒙的核心技术和理念。

③实战化,更贴合企业需求的技术点
学习路线图中的每一个技术点都能够紧贴企业需求,经过多次真实实践,每一个知识点、每一个项目,都是码牛课堂鸿蒙研发团队精心打磨和深度解析的成果,注重对学生的细致教学,每一步都确保学生能够真正理解和掌握。

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:https://qr21.cn/FV7h05

如何快速入门:

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr21.cn/FV7h05

大厂鸿蒙面试题::https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值