【ArkTs语言中的层叠布局】

层叠布局

前言

主要是记录我对openharmony中层叠布局的学习,有错误的地方,还望各位指教

Stack组件(可以包含子组件)

作为容器组件,容器内可以包含各种子元素。其中子元素默认进行居中堆叠

@Entry
@Component
struct StackPage {
  @State num: number = 10;
  build() {
    Column(){
      Stack(){
        Column(){}.width('80%')
        .height('70%')
        .backgroundColor(Color.Brown)
        Text(this.num.toString())
          .fontColor(Color.Black)
          .backgroundColor(Color.Orange)
          .height('40%')
          .width('70%')
          .fontWeight(FontWeight.Bold)
        Button('button')
          .type(ButtonType.Circle)
          .onClick((event: ClickEvent) => {
          if(this.num>0){
            this.num-=2
          }else{
            this.num+=8
          }
        })
          .backgroundColor(Color.Green)
          .width('30%')
          .height('20%')
      }.width('100%')
      .height('100%')
    }
  }
}

在这里插入图片描述

对齐方式

接下来的代码是演示Stack组件的九种对齐方式,代码除了Stack括号中的内容不同外,没有其他的区别

@Entry
@Component
struct StackPage {
  @State num: number = 10;

  build() {
    Column(){
      Stack({alignContent:Alignment.TopStart}){
        Column(){}.width('80%')
        .height('70%')
        .backgroundColor(Color.Brown)
        Text(this.num.toString())
          .fontColor(Color.Black)
          .backgroundColor(Color.Orange)
          .height('40%')
          .width('70%')
          .fontWeight(FontWeight.Bold)
          .align(Alignment.BottomEnd)//设置该容器中的文字的位置
        Button('button')
          .type(ButtonType.Circle)
          .onClick(() => {
          if(this.num>0){
            this.num-=2
          }else{
            this.num+=8
          }
        })
          .backgroundColor(Color.Green)
          .width('30%')
          .height('20%')
      }.width('100%')
      .height('100%')
    }
  }
}

TopStart

在这里插入图片描述

Stack({alignContent:Alignment.Top})

Top

在这里插入图片描述

Stack({alignContent:Alignment.TopEnd})

TopEnd

在这里插入图片描述

 Stack({alignContent:Alignment.Start})

Start

在这里插入图片描述

Stack({alignContent:Alignment.Center})

Center

在这里插入图片描述

  Stack({alignContent:Alignment.End})

End

在这里插入图片描述

Stack({alignContent:Alignment.BottomStart})

BottomStart

在这里插入图片描述

 Stack({alignContent:Alignment.Bottom})

Bottom

在这里插入图片描述

Stack({alignContent:Alignment.BottomEnd})

BottomEnd

在这里插入图片描述

Z序控制

Stack容器中兄弟组件显示层级关系可以通过Z序控制的zIndex属性改变。zIndex值越大,显示层级越高,即zIndex值大的组件会覆盖在zIndex值小的组件上方。

@Entry
@Component
struct StackPage {
  @State num: number = 10;

  build() {
    Column(){
      Stack({alignContent:Alignment.Center}){
        Text('元素1')
          .backgroundColor(Color.Brown)
          .width('30%')
          .height('30%')
          .align(Alignment.BottomEnd)
          .zIndex(3)
        Text('元素2')
          .backgroundColor(Color.Orange)
          .width('50%')
          .height('50%')
          .align(Alignment.BottomEnd)
          .zIndex(2)
        Text('元素3')
          .backgroundColor(Color.Gray)
          .width('70%')
          .height('70%')
          .align(Alignment.BottomEnd)
          .zIndex(1)
      }.width('100%')
      .height('100%')
    }
  }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值