HarmonyOS 组件尺寸显示异常?

如下代码,宽度设置了100%,但实际宽度如截图所示并没有撑满

@Entry
@Component
struct AlignRuleTestPage {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('AlignRuleTestPageHelloWorld')
        .fontSize(30)
        .width("100%")
        .aspectRatio(6)
        .margin({
          top: "14%"
        })
        .backgroundColor(Color.Red)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top },
          left: { anchor: '__container__', align: HorizontalAlign.Start },
          right: { anchor: '__container__', align: HorizontalAlign.End },
        })
    }
    .height('100%')
    .width('100%')
  }
}
阅读 641
1 个回答

请参考以下demo:

@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('AlignRuleTestPageHelloWorld')
        .fontSize(30)//.aspectRatio(0)  去掉
        .margin({
          top: "14%"
        })
        .backgroundColor(Color.Red)
        .fontWeight(FontWeight.Bold)
        .width("100%")
        .height('100%')
    }
    .height('100%')
    .width('100%')
  }
}

aspectRatio的使用参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-layout-constraints-V5

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进