《鸿蒙开发-鸿蒙教程-答案之书》组件margin左和右等于没偏?

《鸿蒙开发-鸿蒙教程-答案之书》组件margin左和右等于没偏?

鸿蒙的margin和Android的margin还是有点区别,Android的margin没有任何毛病,但是鸿蒙margin会导致组件偏移回来的。因为鸿蒙的组件可以超越父组件,往外面宽展。

那怎么办呢?

先看个有问题的代码:

typescript 复制代码
@Entry
@Component
struct TestDel2Page {

  build() {
    Column(){
      Text('123')
        .width('100%')
        .height(30)
        .backgroundColor(Color.Pink)
        .margin({
          right:20,
          left:20
        })
      
    }
    .width('100%')
    .height('100%')
    
  }
}

上面代码你会发现Text的偏移等于没用。

如何修改呢?用constraintSize

修改后的代码如下:

typescript 复制代码
@Entry
@Component
struct TestDel2Page {

  build() {
    Column(){
      Text('123')
        .width('100%')
        .height(30)
        .backgroundColor(Color.Pink)
        .margin({
          right:20,
          left:20
        })
        .constraintSize({
          maxWidth:'100%'
        })

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

  }
}

有鸿蒙开发bug或者需求的可私信我,我每天都看私信的

相关推荐
TMT星球35 分钟前
创梦天地《地铁跑酷》携手鸿蒙 深化全场景生态共建
华为·harmonyos
枫叶丹438 分钟前
【HarmonyOS 6.0】MDM Kit 新特性:PC/2in1设备无锁屏密码重启自动解锁能力详解
开发语言·华为·harmonyos
Davina_yu1 小时前
数据持久化(2):RelationalStore关系型数据库(SQLite)操作(14)
harmonyos·鸿蒙·鸿蒙系统
不良使1 小时前
鸿蒙PC迁移:使用Electron`logseq-master-ohos` 鸿蒙适配全记录
jvm·electron·harmonyos
枫叶丹42 小时前
【HarmonyOS 6.0】MDM Kit:PC/2in1设备用户行为限制策略详解
开发语言·华为·harmonyos
SuperHeroWu72 小时前
【HarmonyOS 7】鸿蒙应用 AI Coding 工具链 DevEco Code 到 DevEco CLI
人工智能·华为·ai编程·harmonyos·cli·code
祭曦念2 小时前
【共创季稿事节】鸿蒙原生 ArkTS 布局深度解析:Column 主轴对齐之 flex-start / center / flex-end 全解
华为·harmonyos
Davina_yu3 小时前
环境变量管理:Environment与LocalStorage的应用场景(23)
harmonyos·鸿蒙·鸿蒙系统
小鹏linux3 小时前
鸿蒙PC迁移:TupiTube Desk Qt 2D 动画工作室鸿蒙PC适配全记录
qt·华为·harmonyos
狼哥16863 小时前
防沉迷控制实战新特性接入
ui·华为·harmonyos