弹性布局例题展示和基础

弹性布局全称弹性盒子布局(Flexible Box Layout,简称 Flex),是一套一维布局模型,核心是让容器内部子元素具备弹性伸缩能力,可灵活控制子组件排列方向、对齐方式、剩余空间分配,完美适配不同尺寸屏幕的响应式界面。 核心特点:

  1. 一维特性:单次仅沿主轴(水平 / 垂直)排布元素;
  2. 弹性伸缩:子元素可自动放大填充空白、缩小避免溢出;
  3. 对齐简便:一键实现水平、垂直居中、均分、两端对齐等复杂排版。

eg:

Entry
@Component
struct FlexDemo{
  build() {
    Flex({direction: FlexDirection.Row,wrap:FlexWrap.Wrap}){
      Text('1').width('49%').height(50).backgroundColor('#F5DEB3')
      Text('2').width('49%').height(50).backgroundColor('#D2B48C')
      Text('3').width('49%').height(50).backgroundColor('#F5DE83')
      Text('4').width('49%').height(50).backgroundColor('#D2B48C')
      Text('5').width('49%').height(50).backgroundColor('#F5DE83')
      Text('6').width('49%').height(50).backgroundColor('#D2B48C')
    }
    .height(70)
    .width('90%')
    .padding(10)
    .backgroundColor('#AFEEEE')
  }
}

成品展示:

eg:

@Entry
@Component
struct FlexDemo2{
  build() {
    Flex({wrap:FlexWrap.Wrap}){
      Text("鸿蒙基础").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
      Text("ArskTS语法").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
      Text("ArkUI布局").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
      Text("组件开发").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
      Text("页面跳转").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
      Text("数据存储").padding(15).backgroundColor(0xE8F4FF).borderRadius(5).fontSize(16).margin(1)
    }
    .width('100%')
    .padding(10)
  }
}

成品展示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值