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

相关推荐
7***374518 分钟前
HarmonyOS分布式能力的核心技术
分布式·华为·harmonyos
不爱吃糖的程序媛1 小时前
Cordova 定位功能在鸿蒙上的实现技术博客
华为·harmonyos
t***L2661 小时前
HarmonyOS国际化
华为·harmonyos
国霄1 小时前
(6)Kotlin/Js For Harmony——ArkTs 开发工具套件
kotlin·harmonyos
奇风2 小时前
uni-app + DevEco 鸿蒙跨平台应用开发实战1-环境安装分享
uniapp·harmonyos·鸿蒙应用开发·鸿蒙跨平台应用开发
爱笑的眼睛112 小时前
HarmonyOS Scroll滚动容器深度性能优化指南
华为·harmonyos
●VON4 小时前
Electron for HarmonyOS 开发环境搭建
javascript·electron·harmonyos
万少5 小时前
上架元服务-味寻纪 技术分享
前端·harmonyos
大雷神6 小时前
windows中flutter开发鸿蒙实操
harmonyos
u***j3248 小时前
HarmonyOS在智能家居中的实践
华为·智能家居·harmonyos