《鸿蒙开发-鸿蒙教程-答案之书》组件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 小时前
HarmonyOS 6 三方SDK对接:从半接模式看Share Kit原理——系统分享的运行机制与设计理念
设计模式·harmonyos·架构设计·分享·半接模式
被温水煮的青蛙2 小时前
HarmonyOS openCustomDialog 实战:从入门到理解原理
harmonyos
高一学习c++会秃头吗2 小时前
鸿蒙适应式布局和响应式布局零基础
harmonyos
HwJack203 小时前
HarmonyOS应用开发中EmbeddedUIExtensionAbility:跨进程 UI 嵌入的“幕后导演“
ui·华为·harmonyos
早點睡3905 小时前
ReactNative项目鸿蒙化三方库集成实战:react-native-calendars(日历展开和日程模块存在兼容性问题)
react native·react.js·harmonyos
糖果墙7 小时前
鸿蒙OLE适配环境搭建
鸿蒙·ole·objecteditor
云和数据.ChenGuang8 小时前
鸿蒙 + ChromaDB:端侧向量检索,打造全场景智能应用新范式
华为·harmonyos·鸿蒙
前端不太难9 小时前
AI + 鸿蒙游戏,会不会是下一个爆点?
人工智能·游戏·harmonyos
Gorit10 小时前
如何使用 Flutter 开发 HarmonyOS 应用
flutter·华为·harmonyos
键盘鼓手苏苏1 天前
Flutter 三方库 p2plib 的鸿蒙化适配指南 - 实现高性能的端到端(P2P)加密通讯、支持分布式节点发现与去中心化数据流传输实战
flutter·harmonyos·鸿蒙·openharmony