IT评测·应用市场-qidao123.com

标题: 【鸿蒙】HarmonyOS NEXT星河入门到实战4-ArkTS界面布局深入 [打印本页]

作者: 篮之新喜    时间: 2024-9-11 15:32
标题: 【鸿蒙】HarmonyOS NEXT星河入门到实战4-ArkTS界面布局深入
目录
一、布局元素组成
1.1 内边距-padding
1.2 外边距 margin
1.3 实战案例-QQ音乐-登录
1.4 边框 border
 二、设置组件圆角
2.1 基本圆角设置
2.2 特殊外形的圆角设置
三、配景属性
3.1 配景图片-backgroundImage
3.2 配景图片位置-backgroundImagePosition
3.3 配景定位-单位题目vp2px(5.x版本忽略此题目,单位一致了)
3.4 配景尺寸大小-backgroundlmageSize
四、线性布局
4.1 主轴对齐方式
 4.2 综合案例 个人中心-顶部导航
4.3 交叉轴对齐方式
4.4 综合案例-得物列表项
4.4 自定义伸缩-layoutWeight
4.5 综合案例-得物卡片
4.6 综合案例-京东登录
五、弹性布局
5.1 主轴方向、对齐方式、交叉轴对齐方式
5.2 综合案例Flex 换行 -wrap
六、绝对定位 -position和层级zlndex
6.1 绝对定位和层级
6.2 综合案例-人气卡片案例
七、层叠布局
7.1 层叠布局示例
7.2 综合案例-B站-视频卡片
八、阶段综合练习-【支付宝】


   媒介:ArkTS界面布局深入讲解
  一、布局元素组成


1.1 内边距-padding


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.      Column(){
  7.        Text('王语嫣')
  8.          .backgroundColor(Color.Pink)
  9.          .padding(10)// 四个方向设置相同的内边距
  10.        Text('杨过')
  11.          .backgroundColor(Color.Green)
  12.          .padding({
  13.            left: 20,
  14.            top: 30,
  15.            right: 5,
  16.            bottom: 30
  17.          })//分别设置
  18.     }
  19.   }
  20. }
复制代码

1.2 外边距 margin


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.      Column(){
  7.        Row(){
  8.          Text('刘备')
  9.            .backgroundColor(Color.Orange)
  10.          Text('关羽')
  11.            .backgroundColor(Color.Pink)
  12.            .margin({
  13.              left: 30,
  14.              right: 30
  15.            })
  16.          Text('张飞')
  17.            .backgroundColor(Color.Green)
  18.        }
  19.        Column(){
  20.          Text('刘备')
  21.            .backgroundColor(Color.Orange)
  22.          Text('关羽')
  23.            .backgroundColor(Color.Pink)
  24.            .margin({
  25.              top: 30,
  26.              bottom: 30
  27.            })
  28.          Text('张飞')
  29.            .backgroundColor(Color.Green)
  30.        }
  31.     }
  32.   }
  33. }
复制代码

1.3 实战案例-QQ音乐-登录


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.      Column(){
  7.        Image($r('app.media.m_user'))
  8.          .width('100')
  9.        Text('女子交友俱乐部')
  10.          .fontWeight(700)
  11.          .margin({
  12.            top: 10,
  13.            bottom: 40
  14.          })
  15.        Button('QQ登录')
  16.          .width('100%')
  17.          .margin({
  18.            bottom: 10
  19.          })
  20.        Button('微信登录')
  21.          .width('100%')
  22.          .backgroundColor('#ddd')
  23.          .fontColor('#000')
  24.     }
  25.     .padding(20)
  26.     .width('100%')
  27.   }
  28. }
复制代码

1.4 边框 border


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.      Column(){
  7.        Text('待完善')
  8.          .fontColor(Color.Red)
  9.          .padding(5)
  10.          .border({
  11.            width: 1,  // 宽度(必须)
  12.            color: Color.Red,
  13.            style: BorderStyle.Dashed  // 样式(Dashed   虚线 Solid  实线(默认) Dotted 点线)
  14.          })
  15.          .margin({bottom: 20})
  16.        Text('单边框')
  17.          .padding(5)
  18.          .border({
  19.            width: {left: 1, right: 2},
  20.            color: {left: Color.Blue, right:  Color.Green},
  21.            style: {left: BorderStyle.Dotted, right: BorderStyle.Solid}
  22.          })
  23.     }
  24.     .padding(20)
  25.     // .width('100%')
  26.   }
  27. }
复制代码

 二、设置组件圆角

2.1 基本圆角设置


  1. import { TextReaderIcon } from '@hms.ai.textReader';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   build() {
  7.      Column(){
  8.        Text('没有圆角')
  9.          .width(100)
  10.          .height(60)
  11.          .backgroundColor(Color.Pink)
  12.          .margin({bottom: 30})
  13.        Text('相同圆角')
  14.          .width(100)
  15.          .height(60)
  16.          .backgroundColor(Color.Gray)
  17.          .margin({bottom: 30})
  18.          .borderRadius(15) // 设置相同的圆角
  19.        Text('不同圆角')
  20.          .width(100)
  21.          .height(60)
  22.          .backgroundColor(Color.Green)
  23.          .borderRadius({
  24.            topLeft: 10,
  25.            topRight:2,
  26.            bottomLeft: 20,
  27.            bottomRight: 30
  28.          }) // 设置不同的圆角
  29.     }
  30.     .padding(20)
  31.     // .width('100%')
  32.   }
  33. }
复制代码

2.2 特殊外形的圆角设置

 


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       // 1 正圆
  8.       Image($r('app.media.cat'))
  9.         .width(100)
  10.         .height(100)
  11.         .borderRadius(50)
  12.         .margin({bottom: 5})
  13.       // 2 胶囊按钮
  14.       Text('胶囊按钮效果与文本长有关')
  15.         .height(50)
  16.         .borderRadius(25)  //高的一半
  17.         .backgroundColor(Color.Pink)
  18.         .padding(10)
  19.     }
  20.     .padding(20)
  21.     // .width('100%')
  22.   }
  23. }
复制代码

三、配景属性


3.1 配景图片-backgroundImage


 

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       Text('中山大道')
  8.         .fontColor(Color.White)
  9.         .width(200)
  10.         .height(100)
  11.         .backgroundColor(Color.Pink)
  12.         .backgroundImage($r('app.media.flower'))
  13.         .margin({bottom: 5})
  14.       Text('背景图片平铺图')
  15.         .fontColor(Color.White)
  16.         .width(200)
  17.         .height(200)
  18.         .backgroundColor(Color.Pink)
  19.         .backgroundImage($r('app.media.flower'),ImageRepeat.XY) // xy水平与垂直平铺
  20.     }
  21.     .padding(20)
  22.     // .width('100%')
  23.   }
  24. }
复制代码

3.2 配景图片位置-backgroundImagePosition



  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       Text()      
  8.         .width(300)
  9.         .height(200)
  10.         .backgroundColor(Color.Pink)
  11.         .backgroundImage($r('app.media.flower'))
  12.         .backgroundImagePosition({x: 50, y: 50}) // 坐标方式
  13.         .margin({bottom: 5})
  14.       Text()
  15.         .fontColor(Color.White)
  16.         .width(300)
  17.         .height(200)
  18.         .backgroundColor(Color.Pink)
  19.         .backgroundImage($r('app.media.flower'))
  20.         .backgroundImagePosition(Alignment.Center)// 方式二 枚举
  21.     }
  22.     .padding(20)
  23.     // .width('100%')
  24.   }
  25. }
复制代码

3.3 配景定位-单位题目vp2px(5.x版本忽略此题目,单位一致了)


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       Text()
  8.         .width(300)
  9.         .height(200)
  10.         .backgroundColor(Color.Pink)
  11.         .backgroundImage($r('app.media.flower'))
  12.         .backgroundImagePosition({
  13.           x:  vp2px(150), //新版5.X 直接150
  14.           y: vp2px(100), //新版5.X 直接100
  15.         })
  16.     }
  17.     .padding(20)
  18.     // .width('100%')
  19.   }
  20. }
复制代码
利用5.X版本,发现花不见了,原来新版本单位已经一致了,无需vp2px

3.4 配景尺寸大小-backgroundlmageSize


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       Text()
  8.         .width(300)
  9.         .height(200)
  10.         .backgroundColor(Color.Green)
  11.         .backgroundImage($r('app.media.jd_bj3'))
  12.         .backgroundImagePosition(Alignment.Center)
  13.         .backgroundImageSize(ImageSize.Cover)  //ImageSize.Contain
  14.         // .backgroundImageSize({
  15.         //   width:300,
  16.         //   height: 200
  17.         // })
  18.     }
  19.     .padding(20)
  20.     // .width('100%')
  21.   }
  22. }
复制代码

四、线性布局


4.1 主轴对齐方式



  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = '春天的菠菜';
  5.   build() {
  6.     Column(){
  7.       Text()
  8.         .width(200)
  9.         .height(100)
  10.         .backgroundColor(Color.Green)
  11.         .border({width: 2})
  12.       Text()
  13.         .width(200)
  14.         .height(100)
  15.         .backgroundColor(Color.Green)
  16.         .border({width: 2})
  17.         .margin(5)
  18.       Text()
  19.         .width(200)
  20.         .height(100)
  21.         .backgroundColor(Color.Green)
  22.         .border({width: 2})
  23.     }
  24.     .width('100%')
  25.     .height('100%')
  26.     .backgroundColor('#ccc')
  27.   //   设置排布主方向的对齐方式(主轴) ctrl + p
  28.   //   Row()方法类似 这里省略
  29.     .justifyContent(FlexAlign.SpaceAround)
  30.   }
  31. }
复制代码

 4.2 综合案例 个人中心-顶部导航


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Row(){
  18.         Image($r('app.media.ic_arrow_left'))
  19.           .width(30)
  20.         Text('个人中心')
  21.         Image($r('app.media.ic_more'))
  22.           .width(24)
  23.       }
  24.       .justifyContent(FlexAlign.SpaceBetween)
  25.       .width('100%')
  26.       .height(40)
  27.       .backgroundColor(Color.White)
  28.       .padding({left: 10,right: 10})
  29.     }
  30.     .width('100%')
  31.     .height('100%')
  32.     .backgroundColor('#ccc')
  33.   }
  34. }
复制代码

4.3 交叉轴对齐方式



  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Text()
  18.         .width(200)
  19.         .height(100)
  20.         .backgroundColor(Color.Green)
  21.         .border({width: 2})
  22.       Text()
  23.         .width(200)
  24.         .height(100)
  25.         .backgroundColor(Color.Green)
  26.         .border({width: 2})
  27.         .margin({top: 5,bottom: 5})
  28.       Text()
  29.         .width(200)
  30.         .height(100)
  31.         .backgroundColor(Color.Green)
  32.         .border({width: 2})
  33.     }
  34.     .width('100%')
  35.     .height('100%')
  36.     .backgroundColor('#ccc')
  37.     .alignItems(HorizontalAlign.Start)  // Row的交叉轴 垂直对齐方式  VerticalAlign.End
  38.   }
  39. }
复制代码

4.4 综合案例-得物列表项



  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Row(){
  18.         // 左侧列
  19.         Column({ space: 8 }){
  20.           Text('玩一玩')
  21.             .fontSize(18)
  22.             .fontWeight(700)
  23.           Text('签到兑礼 | 超多大奖 超好玩')
  24.             .fontSize(12)
  25.             .fontColor('#999')
  26.         }
  27.         .alignItems(HorizontalAlign.Start) // 交叉轴对齐方式
  28.         // 右侧列
  29.         Row({ space: 8 }){
  30.           Image($r('app.media.tree'))
  31.             .width(50)
  32.             .backgroundColor('#efefef')
  33.             .borderRadius(5)
  34.           Image($r('app.media.ic_arrow_right'))
  35.             .width(20)
  36.             .fillColor('#999')
  37.         }
  38.       }
  39.       .justifyContent(FlexAlign.SpaceBetween)
  40.       .padding({left: 15,right:15})
  41.       .width('100%')
  42.       .height(80)
  43.       .backgroundColor('#fff')
  44.       .borderRadius(5)
  45.     }
  46.     .padding(10)
  47.     .width('100%')
  48.     .height('100%')
  49.     .backgroundColor('#ccc')
  50.   }
  51. }
复制代码

4.4 自定义伸缩-layoutWeight


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.     // LayoutWeight
  18.       Row(){
  19.       //   左侧
  20.         Text('左侧')
  21.           .layoutWeight(1)
  22.           .backgroundColor(Color.Green)
  23.           .height(40)
  24.       //   右侧
  25.         Text('右侧固定')
  26.           .width(80)
  27.           .height(40)
  28.           .backgroundColor(Color.Orange)
  29.       }
  30.       .width(300)
  31.       .height(40)
  32.       .backgroundColor('#fff')
  33.       // LayoutWeight 多个对象
  34.       Row(){
  35.         //   老大
  36.         Text('老大')
  37.           .layoutWeight(1)
  38.           .backgroundColor(Color.Green)
  39.           .height(40)
  40.         //   老二
  41.         Text('老二')
  42.           .layoutWeight(2)
  43.           .width(80)
  44.           .height(40)
  45.           .backgroundColor(Color.Pink)
  46.         //   老三
  47.         Text('老三')
  48.           .width(80)
  49.           .height(40)
  50.           .backgroundColor(Color.Orange)
  51.       }
  52.       .width(300)
  53.       .height(40)
  54.       .backgroundColor('#fff')
  55.       .margin({top: 30})
  56.     }
  57.     .padding(10)
  58.     .width('100%')
  59.     .height('100%')
  60.     .backgroundColor('#ccc')
  61.   }
  62. }
复制代码

4.5 综合案例-得物卡片


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Column(){
  18.         Image($r('app.media.nick'))
  19.           .width('100%')
  20.           .borderRadius({topLeft: 5, topRight: 5})
  21.         Text('今晚吃这个 | 每日艺术分享 No. 43')
  22.           .fontWeight(600)
  23.           .fontSize(14)
  24.           .lineHeight(22)
  25.           .height(60)
  26.       //   底部
  27.         Row(){
  28.           //  底部左侧
  29.           Row({space: 5}){
  30.             Image($r('app.media.user'))
  31.               .height(16)
  32.               .borderRadius(8)
  33.             Text('插画师分享聚集地')
  34.               .fontSize(10)
  35.               .fontColor('#999')
  36.           }
  37.           .layoutWeight(1)
  38.           //底部右侧
  39.           Row({space: 5}){
  40.             Image($r('app.media.ic_like'))
  41.               .width(12)
  42.               .fillColor('#999')
  43.             Text('2300')
  44.               .fontSize(10)
  45.               .fontColor('#666')
  46.           }
  47.         }
  48.         .padding({left: 15,right: 15})
  49.       }
  50.       .width(200)
  51.       .padding({bottom: 15})
  52.       // .height(400) // 设计时先固定高,设计完成去掉
  53.       .backgroundColor(Color.White)
  54.       .borderRadius(5)
  55.     }
  56.     .padding(10)
  57.     .width('100%')
  58.     .height('100%')
  59.     .backgroundColor('#ccc')
  60.   }
  61. }
复制代码
 

4.6 综合案例-京东登录

 



  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.     //   顶部区域
  18.       Row(){
  19.         Image($r('app.media.jd_cancel'))
  20.           .width(20)
  21.         Text('帮助')
  22.       }
  23.       .width('100%')
  24.       .justifyContent(FlexAlign.SpaceBetween)
  25.     //   Logo
  26.       Image($r('app.media.jd_logo'))
  27.         .width(250)
  28.         .height(250)
  29.    // 国家地址区域
  30.       Row(){
  31.         Text('国家/地址')
  32.           .layoutWeight(1)
  33.           .fontColor('#666')
  34.         Text('中国(+86)')
  35.           .margin({right: 5})
  36.           .fontColor('#666')
  37.         Image($r('app.media.jd_right'))
  38.           .width(20)
  39.           .fillColor('#666')
  40.       }
  41.       .width('100%')
  42.       .height(40)
  43.       .backgroundColor('#fff')
  44.       .borderRadius(20)
  45.       .padding({left: 15, right:10})
  46.     //   手机号(输入框)
  47.       TextInput({
  48.         placeholder: '请输入手机号'
  49.       })
  50.         .height(40)
  51.         .borderRadius(20)
  52.         .backgroundColor('#fff')
  53.         .margin({top: 20})
  54.         .placeholderColor('#666')
  55.     //   已阅读并同意
  56.       Row(){
  57.         Checkbox()
  58.           .width(10)
  59.           .margin({top: 7})
  60.         // 一段文本中,有文本样式需要单独定制,TEXT 包括SPAN
  61.         Text(){
  62.           Span('我已阅读并同意')
  63.           Span('《京东隐私政策》').fontColor('#3274f6')
  64.           Span('《京东用户服务协议》').fontColor('#3274f6')
  65.           Span('未注册的手机号将自动创建京东账号')
  66.         }
  67.         .fontSize(12)
  68.         .fontColor('#666')
  69.         .lineHeight(20)
  70.       }
  71.       .alignItems(VerticalAlign.Top)
  72.       .margin({top: 20})
  73.     //  登录
  74.       Button('登录')
  75.         .width('100%')
  76.         .backgroundColor('#bf2838')
  77.         .margin({top: 25})
  78.     //   新用户注册等
  79.       Row({ space: 25 }){
  80.         Text('新用户注册').fontSize(14).foregroundColor('#666')
  81.         Text('账户密码登录').fontSize(14).foregroundColor('#666')
  82.         Text('无法登录').fontSize(14).foregroundColor('#666')
  83.       }
  84.       .margin({top: 15})
  85.       // 填充组件Black() 作用 填充空白区域
  86.       Blank()
  87.       // 底部其他登录方式
  88.       Column(){
  89.         Text('其他登录方式')
  90.           .fontColor('#666')
  91.           .height(22)
  92.           .fontSize(14)
  93.           .margin({bottom: 28})
  94.         Row(){
  95.           Image($r('app.media.jd_huawei')).width(34)
  96.           Image($r('app.media.jd_wechat')).width(34).fillColor('#56a44a')
  97.           Image($r('app.media.jd_weibo')).width(34).fillColor('#c8493b')
  98.           Image($r('app.media.jd_QQ')).width(34).fillColor('#4ba0e8')
  99.         }
  100.         .width('100%')
  101.         .margin({bottom: 30})
  102.         .justifyContent(FlexAlign.SpaceAround)
  103.       }
  104.       .width('100%')
  105.       // .backgroundColor(Color.Pink)
  106.     }
  107.     .padding(20)
  108.     .width('100%')
  109.     .height('100%')
  110.     .backgroundColor(Color.Pink)
  111.     .backgroundImage($r('app.media.jd_login_bg'))
  112.     .backgroundImageSize(ImageSize.Cover)
  113.   }
  114. }
复制代码


五、弹性布局


5.1 主轴方向、对齐方式、交叉轴对齐方式

  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     // Flex默认是主轴,水平往右,交叉轴是垂直往下 (跟我们Row相似)
  17.     // 1、 主轴方向
  18.     // direction: FlexDirection.Column  / Row   // 改变主轴方向 {direction: FlexDirection.Column} 改成垂直往下
  19.     // 2、 主轴对齐方式
  20.     // justifyContent: FlexAlign.SpaceAround
  21.     // 3、交叉轴对齐方式
  22.     // alignItems: ItemAlign.End
  23.     Flex({direction: FlexDirection.Row,
  24.       justifyContent: FlexAlign.SpaceAround,
  25.       alignItems: ItemAlign.End
  26.     }){
  27.       Text()
  28.         .width(80).height(80)
  29.         .backgroundColor(Color.Pink)
  30.         .border({width: 1, color: Color.Blue})
  31.       Text()
  32.         .width(80).height(80)
  33.         .backgroundColor(Color.Pink)
  34.         .border({width: 1, color: Color.Blue})
  35.       Text()
  36.         .width(80).height(80)
  37.         .backgroundColor(Color.Pink)
  38.         .border({width: 1, color: Color.Blue})
  39.     }
  40.     .width('100%').height(600)
  41.     .backgroundColor('#5f9a5c')
  42.   }
  43. }
复制代码

5.2 综合案例Flex 换行 -wrap


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Text('阶段选择')
  18.         .fontSize(30)
  19.         .fontWeight(700)
  20.         .padding(15)
  21.         .width('100%')
  22.       Flex({
  23.         wrap: FlexWrap.Wrap
  24.       }){
  25.         Text('ArkUI').margin(5).padding(10).backgroundColor('#f1f1f1')
  26.         Text('ArkTS').margin(5).padding(10).backgroundColor('#f1f1f1')
  27.         Text('界面开发').margin(5).padding(10).backgroundColor('#f1f1f1')
  28.         Text('系统能力').margin(5).padding(10).backgroundColor('#f1f1f1')
  29.         Text('权限控制').margin(5).padding(10).backgroundColor('#f1f1f1')
  30.         Text('元服务').margin(5).padding(10).backgroundColor('#f1f1f1')
  31.       }
  32.     }
  33.   }
  34. }
复制代码

六、绝对定位 -position和层级zlndex

6.1 绝对定位和层级


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Text('张三').width(80).height(80).backgroundColor(Color.Green)
  18.       Text('李四').width(80).height(80).backgroundColor(Color.Yellow)
  19.         .position({ //绝对定位
  20.           x: 50,
  21.           y: 50
  22.         })
  23.         .zIndex(1) //层级
  24.       Text('王二').width(80).height(80).backgroundColor(Color.Orange)
  25.     }
  26.    .width(300).height(300)
  27.     .backgroundColor(Color.Pink)
  28.   }
  29. }
复制代码

6.2 综合案例-人气卡片案例


  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.     // 先整体 再局部 再细节
  18.       Column(){
  19.         // 定位 的VIP
  20.         Text('VIP')
  21.           .position({
  22.             x: 0,
  23.             y: 0
  24.           })
  25.           .zIndex(666)
  26.           .width(40).height(20)
  27.           .backgroundColor('#e49642')
  28.           .borderRadius({
  29.             topLeft: 10,
  30.             bottomRight: 10
  31.           })
  32.           .border({width: 2,color: '#fbe7a3'})
  33.           .fontColor('#fbe7a3')
  34.           .fontStyle(FontStyle.Italic)
  35.           .fontSize(14).fontWeight(700)
  36.           .textAlign(TextAlign.Center) // 文本对齐方式  或者使用 .padding({left: 5})
  37.         // 上图
  38.         Image($r('app.media.position_moco'))
  39.           .width('100%').height(210)
  40.           .borderRadius(10)
  41.       //   下row 图+文本
  42.         Row(){
  43.           Image($r('app.media.position_earphone'))
  44.             .width(20)
  45.             .backgroundColor('#55b7f4')
  46.             .borderRadius(10)
  47.             .padding(3)
  48.             .fillColor(Color.White)
  49.             .margin({left: 6, right: 6})
  50.           Text('飞狗MOCO')
  51.             .fontWeight(700)
  52.         }
  53.         .width('100%').height(30)
  54.         // .backgroundColor(Color.Orange)
  55.       }
  56.       .width(160).height(240)
  57.       .backgroundColor(Color.White)
  58.     }
  59.     .width('100%')
  60.     .height('100%')
  61.     .backgroundColor(Color.Pink)
  62.   }
  63. }
复制代码

七、层叠布局

7.1 层叠布局示例




  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     // 层叠布局
  17.     Stack({
  18.       alignContent: Alignment.Bottom  // 改变位置
  19.     }){
  20.       Text('刘备')
  21.         .width(250).height(250)
  22.         .backgroundColor(Color.Pink)
  23.       Text('关羽')
  24.         .width(150).height(150)
  25.         .backgroundColor(Color.Orange)
  26.       Text('张飞')
  27.         .width(50).height(50)
  28.         .backgroundColor(Color.Yellow)
  29.     }
  30.     .width(300).height(600)
  31.     .backgroundColor(Color.Green)
  32.   }
  33. }
复制代码

7.2 综合案例-B站-视频卡片



  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Column(){
  17.       Column(){
  18.         //  上面图片区域(层叠使用)
  19.         Stack({alignContent: Alignment.Bottom }){
  20.           Image($r('app.media.bz_img'))
  21.             .borderRadius({
  22.               topLeft: 10,
  23.               topRight:10
  24.             })
  25.           Row(){
  26.             Row({space: 5}){
  27.               Image($r('app.media.bz_play'))
  28.                 .width(14)
  29.                 .fillColor(Color.White)
  30.               Text('288万')
  31.                 .fontSize(12)
  32.                 .fontColor(Color.White)
  33.             }
  34.             .margin({right: 10})
  35.             Row({space: 5}){
  36.               Image($r('app.media.bz_msg'))
  37.                 .width(14)
  38.                 .fillColor(Color.White)
  39.               Text('8655')
  40.                 .fontSize(12)
  41.                 .fontColor(Color.White)
  42.             }
  43.             Blank()
  44.             Text('4:33')
  45.               .fontSize(12)
  46.               .fontColor(Color.White)
  47.           }
  48.           .width('100%').height(24)
  49.           .padding({left: 5, right: 5})
  50.           // .backgroundColor(Color.Orange)
  51.         }
  52.         .width('100%').height(125)
  53.         Column(){
  54.           Text('【凤凰传奇新歌】 还原来到国风统治区:唢呐一响神曲《铁衣》,歌声送到了海内外')
  55.             .fontSize(13)
  56.             .lineHeight(16)
  57.             .textOverflow({overflow:TextOverflow.Ellipsis})
  58.             .maxLines(2)
  59.           Row(){
  60.             Text('19万点赞')
  61.               .fontSize(10)
  62.               .fontColor('#e66c43')
  63.               .backgroundColor('#faf0ef')
  64.               .padding(5)
  65.               .borderRadius(2)
  66.             Image($r('app.media.bz_more'))
  67.               .width(14)
  68.           }
  69.           .margin({top: 6})
  70.           .width('100%')
  71.           .justifyContent(FlexAlign.SpaceBetween)
  72.         }
  73.         .padding(5)
  74.       }
  75.       .width(200).height(200)
  76.       .backgroundColor(Color.White)
  77.       .borderRadius(10)
  78.       .margin({top: 10})
  79.     }
  80.     .width('100%').height('100%')
  81.     .backgroundColor('#ccc')
  82.   }
  83. }
复制代码

八、阶段综合练习-【支付宝】


 





 


 
  1. import window from '@ohos.window';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = '春天的菠菜';
  6.   onPageShow(): void {
  7.     window.getLastWindow(AppStorage.get("context"), (err, data) => {
  8.       if (err.code) {
  9.         console.error('Failed to get last window. Cause:' + JSON.stringify(err));
  10.         return;
  11.       }
  12.       data.setFullScreen(true)
  13.     });
  14.   }
  15.   build() {
  16.     Stack({ alignContent: Alignment.Bottom }){
  17.     //   2 主体展示区
  18.       Stack({alignContent: Alignment.Top}){
  19.       //   2.1 头部
  20.         Row(){
  21.           // 左边
  22.           Column(){
  23.             Text('北京').fontSize(18).fontColor('#fff')
  24.             Text('晴 2℃').fontSize(12).fontColor('#fff')
  25.             Image($r('app.media.zfb_head_down'))
  26.               .position({
  27.                 x: 40,
  28.                 y: 0
  29.               })
  30.               .width(12).fillColor('#fff')
  31.           }
  32.         //   中间
  33.           Row(){
  34.             Image($r('app.media.zfb_head_search'))
  35.               .width(20)
  36.               .fillColor('#666')
  37.               .margin({left: 5, right: 5})
  38.             Text('北京交通一卡通')
  39.               .layoutWeight(1)
  40.             Text('搜索')
  41.               .width(55)
  42.               .fontColor('#5b73de')
  43.               .fontWeight(700)
  44.               .textAlign(TextAlign.Center)
  45.               .border({
  46.                 width: {left: 1},
  47.                 color: '#ccc'
  48.               })
  49.           }
  50.           .height(32)
  51.           .layoutWeight(1)
  52.           .backgroundColor(Color.White)
  53.           .borderRadius(5)
  54.           .margin({left: 25, right: 12})
  55.         //   右边
  56.           Image($r('app.media.zfb_head_plus'))
  57.             .width(30)
  58.             .fillColor('#fff')
  59.         }
  60.         .width('100%').height(60)
  61.         .backgroundColor('#5b73de')
  62.         .zIndex(666)
  63.         .padding({left: 10,right: 10})
  64.       //   2.2 主体页面
  65.      Scroll(){
  66.        Column(){
  67.          // TOP快捷按钮区域
  68.         Row(){
  69.           Column(){
  70.             Image($r('app.media.zfb_top_scan'))
  71.               .width(36)
  72.               .fillColor('#fff')
  73.             Text('扫一扫')
  74.               .fontColor('#fff')
  75.           }
  76.           .layoutWeight(1)
  77.           Column(){
  78.             Image($r('app.media.zfb_top_pay'))
  79.               .width(36)
  80.               .fillColor('#fff')
  81.             Text('收付款')
  82.               .fontColor('#fff')
  83.           }
  84.           .layoutWeight(1)
  85.           Column(){
  86.             Image($r('app.media.zfb_top_travel'))
  87.               .width(36)
  88.               .fillColor('#fff')
  89.             Text('出行')
  90.               .fontColor('#fff')
  91.           }
  92.           .layoutWeight(1)
  93.           Column(){
  94.             Image($r('app.media.zfb_top_card'))
  95.               .width(36)
  96.               .fillColor('#fff')
  97.             Text('卡包')
  98.               .fontColor('#fff')
  99.           }
  100.           .layoutWeight(1)
  101.         }
  102.          .backgroundColor('#5b73de')
  103.          .padding({top:5, bottom: 15})
  104.        //   主体区 (背景色#f6f6f6
  105.          Column(){
  106.          //  导航区
  107.            Column({space: 10}){
  108.              Row(){
  109.                Column(){
  110.                  Image($r('app.media.zfb_nav1'))
  111.                    .width(28).margin({bottom: 8})
  112.                  Text('滴滴出行')
  113.                    .fontSize(12).fontColor('#666')
  114.                }
  115.                .layoutWeight(1)
  116.                Column(){
  117.                  Image($r('app.media.zfb_nav2'))
  118.                    .width(28).margin({bottom: 8})
  119.                  Text('生活缴费')
  120.                    .fontSize(12).fontColor('#666')
  121.                }
  122.                .layoutWeight(1)
  123.                Column(){
  124.                  Image($r('app.media.zfb_nav3'))
  125.                    .width(28).margin({bottom: 8})
  126.                  Text('股票')
  127.                    .fontSize(12).fontColor('#666')
  128.                }
  129.                .layoutWeight(1)
  130.                Column(){
  131.                  Image($r('app.media.zfb_nav4'))
  132.                    .width(28).margin({bottom: 8})
  133.                  Text('蚂蚁森林')
  134.                    .fontSize(12).fontColor('#666')
  135.                }
  136.                .layoutWeight(1)
  137.                Column(){
  138.                  Image($r('app.media.zfb_nav5'))
  139.                    .width(28).margin({bottom: 8})
  140.                  Text('手机充值')
  141.                    .fontSize(12).fontColor('#666')
  142.                }
  143.                .layoutWeight(1)
  144.              }
  145.              Row(){
  146.                Column(){
  147.                  Image($r('app.media.zfb_nav6'))
  148.                    .width(28).margin({bottom: 8})
  149.                  Text('余额宝')
  150.                    .fontSize(12).fontColor('#666')
  151.                }
  152.                .layoutWeight(1)
  153.                Column(){
  154.                  Image($r('app.media.zfb_nav7'))
  155.                    .width(28).margin({bottom: 8})
  156.                  Text('花呗')
  157.                    .fontSize(12).fontColor('#666')
  158.                }
  159.                .layoutWeight(1)
  160.                Column(){
  161.                  Image($r('app.media.zfb_nav8'))
  162.                    .width(28).margin({bottom: 8})
  163.                  Text('飞猪旅行')
  164.                    .fontSize(12).fontColor('#666')
  165.                }
  166.                .layoutWeight(1)
  167.                Column(){
  168.                  Image($r('app.media.zfb_nav9'))
  169.                    .width(28).margin({bottom: 8})
  170.                  Text('淘票票')
  171.                    .fontSize(12).fontColor('#666')
  172.                }
  173.                .layoutWeight(1)
  174.                Column(){
  175.                  Image($r('app.media.zfb_nav10'))
  176.                    .width(28).margin({bottom: 8})
  177.                  Text('饿了么')
  178.                    .fontSize(12).fontColor('#666')
  179.                }
  180.                .layoutWeight(1)
  181.              }
  182.              Row(){
  183.                Column(){
  184.                  Image($r('app.media.zfb_nav11'))
  185.                    .width(28).margin({bottom: 8})
  186.                  Text('读书听书')
  187.                    .fontSize(12).fontColor('#666')
  188.                }
  189.                .layoutWeight(1)
  190.                Column(){
  191.                  Image($r('app.media.zfb_nav12'))
  192.                    .width(28).margin({bottom: 8})
  193.                  Text('基金')
  194.                    .fontSize(12).fontColor('#666')
  195.                }
  196.                .layoutWeight(1)
  197.                Column(){
  198.                  Image($r('app.media.zfb_nav13'))
  199.                    .width(28).margin({bottom: 8})
  200.                  Text('直播广场')
  201.                    .fontSize(12).fontColor('#666')
  202.                }
  203.                .layoutWeight(1)
  204.                Column(){
  205.                  Image($r('app.media.zfb_nav14'))
  206.                    .width(28).margin({bottom: 8})
  207.                  Text('医疗健康')
  208.                    .fontSize(12).fontColor('#666')
  209.                }
  210.                .layoutWeight(1)
  211.                Column(){
  212.                  Image($r('app.media.zfb_nav15_more'))
  213.                    .width(28).margin({bottom: 8})
  214.                  Text('更多')
  215.                    .fontSize(12).fontColor('#666')
  216.                }
  217.                .layoutWeight(1)
  218.              }
  219.            }
  220.            .padding(10)
  221.          //   产品区
  222.            Row({space: 5}){
  223.              Image($r('app.media.zfb_pro_pic1'))
  224.                .layoutWeight(1)
  225.              Image($r('app.media.zfb_pro_pic2'))
  226.                .layoutWeight(1)
  227.              Image($r('app.media.zfb_pro_pic3'))
  228.                .layoutWeight(1)
  229.            }
  230.            .padding(10)
  231.            Column({space: 10}){
  232.              Image($r('app.media.zfb_pro_list1'))
  233.                .width('100%')
  234.              Image($r('app.media.zfb_pro_list2'))
  235.                .width('100%')
  236.            }
  237.            .padding(10)
  238.          }
  239.          // .height(1000)
  240.          .width('100%')
  241.          .backgroundColor('#fff')
  242.          .borderRadius({
  243.            topLeft: 20,
  244.            topRight: 20
  245.          })
  246.        }
  247.        .width('100%')
  248.        .padding({top: 60,bottom: 60})
  249.      }
  250.       }
  251.       .width('100%').height('100%')
  252.     //   1 底部Tab导航区
  253.       Row(){
  254.         Column(){
  255.           Image($r('app.media.zfb_tab_home'))
  256.             .width(35)
  257.         }
  258.         .layoutWeight(1)
  259.         Column(){
  260.           Image($r('app.media.zfb_tab_money'))
  261.             .width(28)
  262.             .margin({bottom: 2})
  263.           Text('理财')
  264.             .fontSize(12)
  265.         }
  266.         .layoutWeight(1)
  267.         Column(){
  268.           Image($r('app.media.zfb_tab_life'))
  269.             .width(28)
  270.             .margin({bottom: 2})
  271.           Text('生活')
  272.             .fontSize(12)
  273.         }
  274.         .layoutWeight(1)
  275.         Column(){
  276.           Image($r('app.media.zfb_tab_chat'))
  277.             .width(28)
  278.             .margin({bottom: 2})
  279.           Text('消息')
  280.             .fontSize(12)
  281.         }
  282.         .layoutWeight(1)
  283.         Column(){
  284.           Image($r('app.media.zfb_tab_me'))
  285.             .width(28)
  286.             .margin({bottom: 2})
  287.           Text('我的')
  288.             .fontSize(12)
  289.         }
  290.         .layoutWeight(1)
  291.       }
  292.       .width('100%').height(60)
  293.       .backgroundColor('#fbfcfe')
  294.     }
  295.     .width('100%').height('100%')
  296.     .backgroundColor('#5b73de')
  297.   }
  298. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4