HarmonyOS 宽度100% margin左右不生效?

Column(){
  Shape()
    .margin({left:16,right:16})
    .width('100%')
    .height(0.5)
    .backgroundColor($r('app.color.color_DCDCDC'))
}.height('100%')

这么设置会导致margin不生效,这种如何解决

阅读 468
1 个回答

margin是设置外边距属性,在Column布局中width 100%布局是居中的,此时组件设置margin,导致抵消了没有直观的现象,就会以为margin失效了;padding是设置内边距属性,所以设置.padding({left:16,right:16})可以有明显的现象。

如果想要背影颜色一起生效,需要禁用width(‘100%’)属性

Shape() {
  Rect().width('100%').height(50)
}
.margin({left:16,right:16})
// .width('100%')
.height(80)
.backgroundColor(Color.Green)
.fill(0x317AF7)
.stroke(Color.Black)
.strokeWidth(10)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进