《鸿蒙开发-鸿蒙教程-答案之书》组件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或者需求的可私信我,我每天都看私信的

相关推荐
Z***25803 小时前
HarmonyOS在物联网场景的应用
物联网·华为·harmonyos
Pocker_Spades_A5 小时前
John the Ripper 在 HarmonyOS 上的构建与适配
华为·harmonyos
不爱吃糖的程序媛6 小时前
鸿蒙PC Electron 打印服务实现详解
华为·electron·harmonyos
开源头条9 小时前
2025开源鸿蒙开发者激励计划正式启动,为生态繁荣注入持久动力
华为·开源·harmonyos
奔跑的露西ly12 小时前
【HarmonyOS NEXT】自定义样式复用
华为·harmonyos
lqj_本人12 小时前
HarmonyOS + Cordova:打包发布与环境差异常见问题指南
华为·harmonyos
不羁的木木12 小时前
【开源鸿蒙跨平台开发学习笔记】Day03:React Native 开发 HarmonyOS-GitCode口袋工具开发-1
笔记·学习·harmonyos
lqj_本人12 小时前
鸿蒙Cordova开发踩坑记录:震动反馈的“时差“
华为·harmonyos
lqj_本人14 小时前
鸿蒙原生与Qt混合开发:性能优化与资源管理
qt·harmonyos
lqj_本人15 小时前
鸿蒙Qt字体实战:消灭“豆腐块“乱码与自定义字体加载
qt·华为·harmonyos