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

相关推荐
爱笑的眼睛111 小时前
HarmonyOS通知消息分类管理的深度实践与架构解析
华为·harmonyos
爱笑的眼睛112 小时前
HarmonyOS Menu组件深度自定义:突破默认样式的创新实践
华为·harmonyos
编码追梦人5 小时前
仓颉语言:全栈开发新利器,从服务端到鸿蒙的深度解析与实践
jvm·华为·harmonyos
爱笑的眼睛115 小时前
HarmonyOS输入法框架(IMF)深度解析:构建跨设备智能输入体验
华为·harmonyos
特立独行的猫a5 小时前
鸿蒙应用状态管理新方案:AppStorageV2与PersistenceV2深度详解
华为·harmonyos·状态管理·appstoragev2·persistencev2
奔跑的露西ly6 小时前
【HarmonyOS NEXT】Navigation路由导航
华为·harmonyos
坚果的博客7 小时前
Cordova 开发鸿蒙应用完全指南
华为·harmonyos
爱笑的眼睛119 小时前
HarmonyOS应用开发中HTTP网络请求的封装与拦截器深度实践
华为·harmonyos
爱笑的眼睛1111 小时前
HarmonyOS截屏与录屏API深度解析:从系统权限到像素流处理
华为·harmonyos
Android疑难杂症12 小时前
鸿蒙Notification Kit通知服务开发快速指南
android·前端·harmonyos