HarmonyOS开发:ArkUI相对布局RelativeContainer解决层级嵌套较多问题

相对布局(RelativeContainer)

RelativeContainer相对布局的特是,RelativeContainer布局内的子组件可以已父组件或者兄弟组件为锚点来进行相对定位。

如下图所示:

  • 子组件Item1 相对于父组件RelativeContainer确定自己位置。
  • 子组件Item2 相对于兄弟组件 Item1 确定自己位置。

通过给子组件配置alignRules(value: LocalizedAlignRuleOptions) 来设置对齐规则。

设置对齐规则有如下必要点:

  • 确定子组件的参考边线条:左、上、右、下、垂直中线、水平中线
  • 确定锚点:父组件的锚点默认为__container__
  • 确定对齐方式:与父组件的那条对齐(上、中、下、左、中、右)

参考边线

对于RelativeContainer的任何一个子组件而言,它都有6条边线,分别时start、middle、end、top、center、bottom ,设置当前组件的哪个边界对齐到锚点,就可以确定自己在位置。

如图所示

锚点

锚点设置涉及子元素相对于其父元素或兄弟元素的位置依赖关系。具体而言,子元素可以将其位置锚定到相对布局容器(RelativeContainer)、辅助线(guideline)、屏障(barrier)或其他子元素上。

为了准确定义锚点,RelativeContainer的子元素必须拥有唯一的组件标识,用于指定锚点信息。父元素RelativeContainer的标识默认为__container__,其他子元素的组件标识,则通过id属性设置。

示例1:给RelativeContainer里面的子组件Row()设置对齐规则

  • 左边与父组件的左边对齐
  • 下班与父组件的下边对齐
ts{3-6} 复制代码
RelativeContainer() {
	Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        start: { anchor: '__container__', align: HorizontalAlign.Start },
        bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
      })
}
.width(300)
.height(300)
.borderWidth(1)

示例:给RelativeContainer的四个角,以及中间放置元素。

对于任何一个组件而言,它有6条线,分别时start、middle、end、top、center、bottom 通过这6条线与父组件或者其他兄弟组件设置对齐方式,就可以确定自己在位置。

示例代码如下

ts 复制代码
 RelativeContainer() {
    //对齐规则:左上对齐
    Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        start: { anchor: '__container__', align: HorizontalAlign.Start },
        top: { anchor: '__container__', align: VerticalAlign.Top }
      })
	
    //对齐规则:右上对齐
    Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        end: { anchor: '__container__', align: HorizontalAlign.End },
        top: { anchor: '__container__', align: VerticalAlign.Top }
      })

    //对齐规则:左下对齐
    Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        start: { anchor: '__container__', align: HorizontalAlign.Start },
        bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
      })

    //对齐规则:右下对齐
    Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        end: { anchor: '__container__', align: HorizontalAlign.End },
        bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
      })
	
    //对齐规则:水平垂直居中
    Row().width(100).height(100).backgroundColor(Color.Green)
      .alignRules({
        middle: { anchor: '__container__', align: HorizontalAlign.Center },
        center: { anchor: '__container__', align: VerticalAlign.Center }
      })
  }
  .width(300)
  .height(300)
  .borderWidth(1)

相对于兄弟组件定位

相对于兄弟组件的定位时,需要给兄弟组件设置id,然后锚点指向兄弟组件的id即可。

示例代码

ts 复制代码
RelativeContainer() {
    Row().width(100).height(100).backgroundColor(Color.Red)
      .alignRules({
        start: { anchor: '__container__', align: HorizontalAlign.Start },
        top: { anchor: '__container__', align: VerticalAlign.Top }
      })
      .id("id1")

    Row().width(100).height(100).backgroundColor(Color.Green)
      .alignRules({
        //左边与id1的右边对齐
        //上边与id1的下边对齐
        start: { anchor: 'id1', align: HorizontalAlign.End },
        top: { anchor: 'id1', align: VerticalAlign.Bottom }
      })
}
.width(300)
.height(300)
.borderWidth(1)

效果如下图所示

设置偏移量

当通过alignRules设置相对位置还达不到目的时,可以使用offset设置编译量进一步调整。offset是相对于组件原来的位置进行偏移。

如图所示:对绿色块向上和左分偏移30个像素和50个像素。

示例代码:

ts 复制代码
RelativeContainer() {
        Row()
          .width(100)
          .height(100)
          .backgroundColor(Color.Red)
          .alignRules({
            start: { anchor: '__container__', align: HorizontalAlign.Start },
            top: { anchor: '__container__', align: VerticalAlign.Top }
          })
          .id("id1")

        Row()
          .width(100)
          .height(100)
          .backgroundColor(Color.Green)
          .alignRules({
            //左边与id1的右边对齐
            //上边与id1的下边对齐
            start: { anchor: 'id1', align: HorizontalAlign.End },
            top: { anchor: 'id1', align: VerticalAlign.Bottom }
          })
          .offset({
            x: -50,	//向左偏移50个像素
            y: -30	//向上偏移30个像素
          })
      }
      .width(300)
      .height(300)
      .borderWidth(1)

到此,相对布局的用法就介绍完了。撒花🎉🎉🎉

相关推荐
天聚数行3 小时前
华为鸿蒙系统(HarmonyOS)调用天聚数行 API 教程
华为·php·harmonyos·tianapi·天聚数行
BlackWolfSky4 小时前
鸿蒙加解密
华为·harmonyos
融云4 小时前
融云 4 款 SDK 首批通过 GIIC 鸿蒙生态评测
华为·harmonyos
讯方洋哥5 小时前
HarmonyOS应用开发—页面路由
华为·harmonyos
鸿蒙开发工程师—阿辉5 小时前
HarmonyOS 5 高效使用命令:HDC 基本使用
华为·harmonyos
梧桐ty6 小时前
鸿蒙 + Flutter:构建万物互联时代的跨平台应用新范式
flutter·华为·harmonyos
梧桐ty7 小时前
鸿蒙 + Flutter:破解“多端适配”困局,打造万物互联时代的高效开发范式
flutter·华为·harmonyos
Python私教7 小时前
鸿蒙应用的测试和调试完全指南:从单元测试到性能分析
华为·单元测试·harmonyos
Archilect7 小时前
终点、光圈与 Lottie 生命周期:复杂关闭动效的「收尾工程」
harmonyos
鸿蒙开发工程师—阿辉7 小时前
HarmonyOS 5 高效使用命令:HDC 使用指南
华为·harmonyos