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

相关推荐
赵财猫._.2 小时前
Native API开发:C++与ArkTS混合编程实战
开发语言·c++·harmonyos
熊猫钓鱼>_>6 小时前
从零到一:使用 ArkTS 构建你的第一个鸿蒙应用
华为·移动开发·harmonyos·arkts·鸿蒙·component·网页开发
lqj_本人9 小时前
鸿蒙原生API与OnsenUI组件的双向通信实现
华为·harmonyos
开开心心_Every13 小时前
Excel图片提取工具,批量导出无限制
学习·pdf·华为云·.net·excel·harmonyos·1024程序员节
爱笑的眼睛1114 小时前
深入理解ArkTS类的继承与多态:构建灵活可扩展的HarmonyOS应用
华为·harmonyos
爱笑的眼睛1114 小时前
HarmonyOS Toggle开关组件的深度状态管理实践
华为·harmonyos
ChinaDragon15 小时前
HarmonyOS:绘制几何图形 (Shape)
harmonyos
Kisang.16 小时前
【HarmonyOS】ArkWeb——从入门到入土
前端·华为·typescript·harmonyos·鸿蒙
ChinaDragon18 小时前
HarmonyOS:弹出框蒙层控制
harmonyos
大咖分享课19 小时前
HarmonyOS 6 有哪些新变化、新功能?
华为·harmonyos