编程技术分享平台

网站首页 > 技术教程 正文

鸿蒙Harmony OS Next原生开发修改Hello World案例了解Column和Row

xnh888 2024-11-08 14:43:47 技术教程 45 ℃ 0 评论

今天带大家在Hello World中尝试修改对应的页面布局效果

首先,我们再添加一行Text组件,让它同时存在两行

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
 
  build() {
    Row() {
      Column(){
        Text(this.message)
          .id('HelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          });
        Text(this.message)
          .id('HelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          });
      }.width('100%');
    }
    .height('100%')
    .width('100%');
  }
}

事实证明Column()组件就是竖向排列的,其中的元素组件也如预期般排布了

接下来我们调整一下左右,让左右两边都有这个Hello World效果,让它们左右排布

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
 
  build() {
    Row() {
      Column(){
        Text(this.message)
          .id('HelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          });
    }.width('50%');
      Column(){
        Text(this.message)
          .id('HelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          });
      }.width('50%');
    }.height('100%');
  }
}

确实实现了我们想要的效果

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表