ArkUI 开发框架提供了 RelativeContainer 组件实现相对布局的能力,该布局适用于复杂场景下多元素对齐的情况。该组件可以包含多个子组件,本节笔者简单介绍一下 RelativeContainer 的使用。
interface RelativeContainerInterface {
(): RelativeContainerAttribute;
}
declare class RelativeContainerAttribute extends CommonMethod<RelativeContainerAttribute> {
}
RelativeContainer 默认构造方法无需任何额外参数,也没有定义额外的属性方法,简单样例如下所示:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("text1")
.fontSize(25)
.width(160)
.height(150)
.id("text1") // 必须添加 id,否则不显示
.textAlign(TextAlign.End)
.backgroundColor("#ccaabb")
Text("text2")
.fontSize(25)
.width(90)
.id("text2") // 必须添加 id,否则不显示
.textAlign(TextAlign.Start)
.backgroundColor("#bbccaa")
Text("text3")
.fontSize(25)
.width(90)
.margin({top: 50})
// .id("text3") // 注释掉id,组件不显示
.textAlign(TextAlign.Start)
.backgroundColor("#bbccaa")
}
.width("100%")
.height(190)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding(10)
}
}
样例运行结果如下图所示:
📢:由运行结果可知 RelativeContainer 在默认情况下,子组件是按照堆叠排列并沿着自身左上角对齐。
RelativeContainer 对子组件的对齐方式分为水平方向和竖直方向:
设置水平方向对齐
RelativeContainer 的子组件在水平方向对齐方式分为 left 、middle 和 right,分别说明如下:
left: 设置子组件在水平方向上 左边框 的对齐方式,可选值说明如下:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("Start")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
left: {
anchor: "__container__",
align: HorizontalAlign.Start
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Center")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
left: {
anchor: "__container__",
align: HorizontalAlign.Center
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("End")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
left: {
anchor: "__container__",
align: HorizontalAlign.End
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding({left: 50, top: 10, right: 50, bottom: 10})
}
}
样例运行结果如下图所示:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("Start")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
right: {
anchor: "__container__",
align: HorizontalAlign.Start
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Center")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
right: {
anchor: "__container__",
align: HorizontalAlign.Center
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("End")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
right: {
anchor: "__container__",
align: HorizontalAlign.End
}
})
.id("test")
}
.width("100%")
.height(70)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding({left: 50, top: 10, right: 50, bottom: 10})
}
}
样例运行结果如下图所示:
RelativeContainer 的子组件在竖直方向对齐配置分为:top、center 和 bottom,分别说明如下:
top: 设置子组件在竖直方向上 上边框 的对齐方式,可选值说明如下:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("Top")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
top: {
anchor: "__container__",
align: VerticalAlign.Top
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Center")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
top: {
anchor: "__container__",
align: VerticalAlign.Center
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Bottom")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
top: {
anchor: "__container__",
align: VerticalAlign.Bottom
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding({left: 50, top: 10, right: 50, bottom: 10})
}
}
样例运行结果如下图所示:
center: 设置子组件在竖直方向上 中间点 的对齐方式,可选值说明如下:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("Top")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
center: {
anchor: "__container__",
align: VerticalAlign.Top
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Center")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
center: {
anchor: "__container__",
align: VerticalAlign.Center
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Bottom")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
center: {
anchor: "__container__",
align: VerticalAlign.Bottom
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding({left: 50, top: 50, right: 50, bottom: 10})
}
}
下边框 的对齐方式,可选值说明如下:
@Entry @Component struct ArkUIClubTest {
build() {
Column({space: 10}) {
RelativeContainer() {
Text("Top")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
bottom: {
anchor: "__container__",
align: VerticalAlign.Top
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Center")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
bottom: {
anchor: "__container__",
align: VerticalAlign.Center
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
RelativeContainer() {
Text("Bottom")
.fontSize(25)
.width(120)
.height(40)
.backgroundColor("#bbccaa")
.alignRules({
bottom: {
anchor: "__container__",
align: VerticalAlign.Bottom
}
})
.id("test")
}
.width("100%")
.height(90)
.backgroundColor(Color.Pink)
}
.width("100%")
.height("100%")
.backgroundColor("#aabbcc")
.padding({left: 50, top: 50, right: 50, bottom: 10})
}
}
📢:注意事项:
__container__。@Entry @Component struct ArkUIClubTest {
build() {
Column() {
RelativeContainer() {
Row()
.width(100)
.height(100)
.backgroundColor("#FF3333")
.alignRules({
top: {
anchor: "__container__",
align: VerticalAlign.Top
},
left: {
anchor: "__container__",
align: HorizontalAlign.Start
}
})
.id("row1")
Row()
.width(100)
.height(100)
.backgroundColor("#FFCC00")
.alignRules({
top: {
anchor: "__container__",
align: VerticalAlign.Top
},
right: {
anchor: "__container__",
align: HorizontalAlign.End
}
})
.id("row2")
Row().height(100)
.backgroundColor("#FF6633")
.alignRules({
top: {
anchor: "row1",
align: VerticalAlign.Bottom
},
left: {
anchor: "row1",
align: HorizontalAlign.End
},
right: {
anchor: "row2",
align: HorizontalAlign.Start
}
})
.id("row3")
Row()
.backgroundColor("#FF9966")
.alignRules({
top: {
anchor: "row3",
align: VerticalAlign.Bottom
},
bottom: {
anchor: "__container__",
align: VerticalAlign.Bottom
},
left: {
anchor: "__container__",
align: HorizontalAlign.Start
},
right: {
anchor: "row1",
align: HorizontalAlign.End
}
})
.id("row4")
Row()
.backgroundColor("#FF66FF")
.alignRules({
top: {
anchor: "row3",
align: VerticalAlign.Bottom
},
bottom: {
anchor: "__container__",
align: VerticalAlign.Bottom
},
left: {
anchor: "row2",
align: HorizontalAlign.Start
},
right: {
anchor: "__container__",
align: HorizontalAlign.End
}
})
.id("row5")
}
.width(300)
.height(300)
.border({ width: 3, color: "#6699FF" })
}
.height('100%')
.width("100%")
.padding(10)
}
}
码牛课堂也为了积极培养鸿蒙生态人才,让大家都能学习到鸿蒙开发最新的技术,针对一些在职人员、0基础小白、应届生/计算机专业、鸿蒙爱好者等人群,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线。大家可以进行参考学习:
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计0页),希望对大家有所帮助:
如何快速入门:
开发基础知识:
基于ArkTS 开发:
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务