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

相关推荐
xiaocao_10234 小时前
鸿蒙手机上使用的备忘录怎么导出来?
华为·智能手机·harmonyos
春卷同学6 小时前
打砖块 - Electron for 鸿蒙PC项目实战案例
android·electron·harmonyos
春卷同学9 小时前
Electron for鸿蒙PC开发的骰子游戏应用
游戏·electron·harmonyos
春卷同学9 小时前
Electron for 鸿蒙pc开发的二十一点游戏
游戏·electron·harmonyos
不老刘10 小时前
react native for OpenHarmony iconfont 图标不显示问题
react native·harmonyos·iconfont
kirk_wang10 小时前
Flutter三方库鸿蒙适配深度解析:从架构原理到性能优化实践
flutter·移动开发·跨平台·arkts·鸿蒙
汉堡黄•᷄ࡇ•᷅10 小时前
鸿蒙开发: 案例集合List:ListItem侧滑(删除、收藏)
harmonyos·鸿蒙·鸿蒙系统
春卷同学10 小时前
拼图游戏 - Electron for 鸿蒙PC项目实战案例
javascript·electron·harmonyos
春卷同学11 小时前
基于 Electron 开发的跨平台鸿蒙PC端数字猜谜游戏桌面应用
游戏·electron·harmonyos
盐焗西兰花11 小时前
鸿蒙学习实战之路 - 轮播图组件实现
学习·华为·harmonyos