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

相关推荐
安卓开发者1 天前
鸿蒙NEXT应用接入快捷栏:一键直达,提升用户体验
java·harmonyos·ux
HMS Core1 天前
消息推送策略:如何在营销与用户体验间找到最佳平衡点
华为·harmonyos·ux
Brianna Home1 天前
【案例实战】鸿蒙分布式调度:跨设备协同实战
华为·wpf·harmonyos
Bert丶seven1 天前
鸿蒙Harmony实战开发教学(No.4)-RichText组件基础到高阶介绍篇
华为·harmonyos·arkts·鸿蒙·鸿蒙系统·arkui·开发教程
鸿蒙小白龙1 天前
openharmony之分布式蓝牙实现多功能场景设备协同实战
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
爱吃水蜜桃的奥特曼1 天前
玩Android Harmony next版,通过项目了解harmony项目快速搭建开发
android·harmonyos
鸿蒙小白龙1 天前
openharmony之分布式购物车开发实战
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
鸿蒙小白龙1 天前
openharmony之分布式相机开发:预览\拍照\编辑\同步\删除\分享教程
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
安卓开发者1 天前
鸿蒙NEXT鼠标光标开发完全指南
华为·计算机外设·harmonyos
●VON1 天前
重生之我在大学自学鸿蒙开发第九天-《分布式流转》
学习·华为·云原生·harmonyos·鸿蒙