HarmonyOS4.0 ArkUI构建布局

一、线性布局

在这里插入图片描述

属性说明:

  • justifyContent:设置子元素在主轴方向的对齐方式
    • 参数:FlexAlign枚举
  • alignItems:设置子元素在交叉轴方向的对齐方式
    • 参数:
      • Row容器使用VerticalAlign枚举
      • Column容器使用HorizontalAlign枚举

1、Column布局

1.1、FlexAlign

在这里插入图片描述

1.2、HorizontalAlign

在这里插入图片描述

1.3、代码示例

在这里插入图片描述

@Entry
@Component
struct ColumnUI {
  build() {

    // space:设置内容间隔
    Column({ space: 50 }) {
      Text("文本内容一")
      Text("文本内容二")
      Text("文本内容三")
      Text("文本内容四")
    }
    .borderColor("red")
    .width("100%")
    .height("100%")
    .borderWidth(2)
    .backgroundColor("green")
    // 主轴方向的对齐方式
    .justifyContent(FlexAlign.Center)
    // 交叉轴方向的对齐方式
    .alignItems(HorizontalAlign.Center)

  }
}

2、Row布局

2.1、FlexAlign

在这里插入图片描述

2.2、VerticalAlign

在这里插入图片描述

2.3、代码示例

在这里插入图片描述

@Entry
@Component
struct RowUI {

  build() {
    Row() {
      Text("文本内容一")
      Text("文本内容二")
    }
    .height('100%')
    .width("100%")
    .borderWidth(2)
    .borderColor("red")
    .backgroundColor("green")
    .justifyContent(FlexAlign.Center)
    .alignItems(VerticalAlign.Top)
  }
}

3、需求

在这里插入图片描述

二、列表布局

List是一种复杂布局,具备以下特点:

① 列表项(ListItem)数量过多超出屏幕后,会自动提供滚动功能

② 列表项(ListItem)既可以纵向排列,也可以横向排列

语法:

List({space:3}){
    ListItem(){
        Text("列表项")
    }
    
     ListItem(){
        Text("列表项")
    }
}
class Shops{
  name:string
  img:ResourceStr
  price:number
  status:number

  constructor(name:string,img:ResourceStr,price:number,status:number) {
    this.name = name
    this.img = img
    this.price = price
    this.status = status
  }
}
@Entry
@Component
struct ListUi {

  private shops:Array<Shops> = [
    new Shops("华为mate70旗舰手机",$r("app.media.down"),8999,500),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),9999,200),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0),
    new Shops("华为mate70旗舰手机",$r("app.media.down"),10999,0)
  ]

  build() {




    Column(){
      Row(){
        Text("商品列表")
          .fontSize(30)
      }
      .width("90%")
      .height("60")

      List(){
        ForEach(this.shops,(item:Shops) =>{
          ListItem(){
            Row(){
              Image(item.img)
                .width(100)

              Column({space:4}){

                if (item.status) {
                  Text(item.name)
                    .fontWeight(FontWeight.Bold)
                  Text("原价:¥"+item.price)
                    .fontColor("#c7c7c7")
                    .decoration({type:TextDecorationType.LineThrough})
                    .fontSize(14)
                  Text("折扣价:¥"+(item.price - item.status))
                    .fontColor("red")
                  Text("补贴:¥"+item.status)
                    .fontColor("red")
                } else{
                  Text(item.name)
                    .fontWeight(FontWeight.Bold)
                  Text("¥"+item.price)
                    .fontColor("red")
                }
              }
              .alignItems(HorizontalAlign.Start)
            }
            .width("90%")
            .backgroundColor("#ffffff")
            .height("15%")
            .borderRadius(8)
            .margin({bottom:10})
          }

        })
      }
    }
    .width("100%")
    .height("100%")
    .backgroundColor("#c7c7c7")
  }
}

15%")
.borderRadius(8)
.margin({bottom:10})
}

    })
  }
}
.width("100%")
.height("100%")
.backgroundColor("#c7c7c7")

}
}


![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/f8c5053c44c945c3afbe643c9842f877.png#pic_center)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘程云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值