【鸿蒙应用开发】常见的容器组件:ColumnSplit、RowSplit和Flex

上一章已经了解了Column和Row的一些属性,以下是几个案例:

设置子组件水平方向的间距为:5

@Entry
@Preview
@Component
struct Index {
  @State message: string = 'Hello 鸿蒙';
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Row({ space: 5 }) {
        Row()
          .width("30%")
          .height(50)
          .backgroundColor("red")
        Row()
          .width("30%")
          .height(50)
          .backgroundColor("blue")
      }
      .width("90%")
      .height('100%')
      .border({ width: 1 })

    }
    .height('100%')
    .width('100%')
  }
}

效果如图所示:
在这里插入图片描述

设置子元素垂直方向上的对齐方式:

@Entry
@Preview
@Component
struct Index {
  @State message: string = 'Hello 鸿蒙';
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Row({ space: 5 }) {
        Row()
          .width("30%")
          .height(50)
          .backgroundColor("red")
        Row()
          .width("30%")
          .height(50)
          .backgroundColor("blue")
      }
      .width("90%")
      .height('100%')
      .border({ width: 1 })
      .alignItems(VerticalAlign.Center)
    }
    .height('100%')
    .width('100%')
  }
}

效果如图所示:

在这里插入图片描述
其他的对齐方式,选择对应的属性即可。

ColumnSplit和RowSplit

ColumnSplit和RowSplit是在每一个子组件之间插入一条分隔线,ColumnSplit是横向分隔线,RowSplit是纵向的分隔线。

RowSplit使用方法如下:

@Entry
@Preview
@Component
struct Index {
  @State message: string = 'Hello 鸿蒙';
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      RowSplit(){
        Text("1")
          .width(100)
          .height(30)
          .backgroundColor(0xF5DEB3)
        Text("1")
          .width(100)
          .height(30)
          .backgroundColor(0xF5DEB3)
        Text("1")
          .width(100)
          .height(30)
          .backgroundColor(0xF5DEB3)
      }
      .resizeable(true) // 是否可拖动
      .width("90%").height(400)
    }
    .height('100%')
    .width('100%')
  }
}

效果如图:在这里插入图片描述
ColumnSplit的用法也是类似的。

Flex

Flex组件是以弹性方式布局子元素,标准的Flex布局容器包含以下参数:

  • direction:子组件在 flex 容器上排列的方向,也就是主轴方向。
  • wrap:Flex 容器以单行/列 还是多行/列排列。
  • justifyContent:子组件在 Flex 容器主轴上的对齐格式
  • alignItems:子组件在 Flex 容器交叉轴的对齐方式
  • alignContent:交叉轴上有额外空间的时候,多行内容的对齐方式,只有在 wrap 为Wrap或WrapReverse时生效。

比如:

      Flex({direction:FlexDirection.Row}){
        Text("1").width(100).height(40).backgroundColor(0xF5DEB3)
        Text("1").width(100).height(40).backgroundColor(0xF5DEB3)
        Text("1").width(100).height(40).backgroundColor(0xF5DEB3)
        Text("1").width(100).height(40).backgroundColor(0xF5DEB3)
      }

效果如图:在这里插入图片描述

今天先总结ColumnSplit、RowSplit和Flex三个组件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

读心悦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值