自适应布局
1. 拉伸能力
拉伸能力通过flexGrow和flexShrink属性实现,flexGrow拉伸,flexShrink收缩,通过给定份数,实现收或缩所占相对父容器的大小
Row(){
Row()
.width(200)
.height(200)
.backgroundColor(Color.Red)
.border({ width: 4, color: Color.Grey })
.flexGrow(2)
.flexShrink(1)
Row()
.width(200)
.height(200)
.backgroundColor(Color.Pink)
.border({ width: 4, color: Color.Grey })
.flexShrink(2)
.flexGrow(1)
}
2. 均分能力
均分能力通过容器组件的justifyContent属性实现,其属性参数的FlexAlign枚举,实现三种不同的均分能力
●SpaceBetween:父组件的首尾子组件与父组件首尾对齐,其他子组件平均分配剩余空间
●SpaceAround:每个子组件左右各有间隙,导致相邻间隙是头部或尾部间隙的2倍
●SpaceEvenly:子组件在父组件中均匀分布,子组件间隔相等
@Builder
aroundBox() {
Column({ space: 10 }) {
Text('around')
.fontSize(26)
Row() {
Row()
.width(100)
.height(100)
.backgroundColor(Color.Brown)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Blue)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Green)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Orange)
}
.width("100%")
.justifyContent(FlexAlign.SpaceAround)
}
}
@Builder
bewteenBox() {
Column({ space: 10 }) {
Text('bewteen')
.fontSize(26)
Row() {
Row()
.width(100)
.height(100)
.backgroundColor(Color.Brown)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Blue)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Green)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Orange)
}
.width("100%")
.justifyContent(FlexAlign.SpaceBetween)
}
}
@Builder
evenlyBox() {
Column({ space: 10 }) {
Text('evenly')
.fontSize(26)
Row() {
Row()
.width(100)
.height(100)
.backgroundColor(Color.Brown)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Blue)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Green)
Row()
.width(100)
.height(100)
.backgroundColor(Color.Orange)
}
.width("100%")
.justifyContent(FlexAlign.SpaceEvenly)
}
}
3.占比能力
占比能力通过子组件宽度百分比或者父组件(容器组件)的layout属性实现
@Builder
percentageBuilder() {
Text('百分比')
.fontSize(22)
Row() {
Row()
.width('33.33%')
.height(100)
.backgroundColor(Color.Red)
.border({ width: 4, color: Color.Grey })
Row()
.width('33.33%')
.height(100)
.backgroundColor(Color.Pink)
.border({ width: 4, color: Color.Grey })
Row()
.width('33.33%')
.height(100)
.backgroundColor(Color.Green)
.border({ width: 4, color: Color.Grey })
}
.width(this.containVal)
this.sliderBuilder()
}
@Builder
layoutWeightBuilder() {
Text('layoutWeight')
.fontSize(22)
Row() {
Row()
.layoutWeight(1)
.height(100)
.backgroundColor(Color.Red)
.border({ width: 4, color: Color.Grey })
Row()
.layoutWeight(1)
.height(100)
.backgroundColor(Color.Pink)
.border({ width: 4, color: Color.Grey })
Row()
.layoutWeight(1)
.height(100)
.backgroundColor(Color.Green)
.border({ width: 4, color: Color.Grey })
}
.width(this.containVal)
this.sliderBuilder()
}
4. 缩放能力
缩放能力通过aspectRatio属性通过设置宽高比,保持图片的完整显示
Image($r('app.media.ic_video_grid_1'))
.width('100%')
.height('100%')
}
// .aspectRatio(1 / 1)
// .aspectRatio(1 / 2)
.aspectRatio(1072 / 616)
.border({ width: 2, color: "#66F1CCB8" })
5. 延伸能力
延伸能力通过List组件和scroll组件实现,超出的内容隐藏,滚动显示,根据容器尺寸的内容显示多个内容
List({ space: 10 }) {
ForEach(this.appList, (item: NavItem) => {
ListItem() {
Column() {
Image(item.icon)
.width(48)
.height(48)
.margin({ top: 8 })
Text(item.title)
.width(64)
.height(30)
.lineHeight(15)
.fontSize(12)
.textAlign(TextAlign.Center)
.margin({ top: 8 })
.padding({ bottom: 15 })
}
.width(80)
.height(102)
}
.width(80)
.height(102)
})
}
.listDirection(Axis.Horizontal)
.padding({ top: 16, left: 10 })
.width('100%')
.height(118)
.borderRadius(16)
.backgroundColor(Color.White)
scss 代码解读复制代码Scroll() {
Row({ space: 10 }) {
ForEach(this.appList, (item: NavItem, index: number) => {
Column() {
Image(item.icon)
.width(48)
.height(48)
.margin({ top: 8 })
Text(item.title)
.width(64)
.height(30)
.lineHeight(15)
.fontSize(12)
.textAlign(TextAlign.Center)
.margin({ top: 8 })
.padding({ bottom: 15 })
}
.width(80)
.height(102)
})
}
}
.scrollable(ScrollDirection.Horizontal)
.edgeEffect(EdgeEffect.Fade)
.padding({ top: 16, left: 10 })
.height(118)
.borderRadius(16)
.backgroundColor(Color.White)
6. 隐藏能力
隐藏能力通过组件的displayPriority属性实现,属性设置权重,当父组件大小不足时,权重高的组件优先显示,权重相同的,会一起显示
Image($r("app.media.ic_public_favor"))
.width(48)
.height(48)
.displayPriority(1)
Image($r("app.media.ic_public_play_last"))
.width(48)
.height(48)
.displayPriority(2)
Image($r("app.media.ic_public_pause"))
.width(48)
.height(48)
.displayPriority(3)
7. 折行能力
折行能力通过Flex组件的wrap属性实现,实现一行无法展示,转换成多行展示
Flex({
direction: FlexDirection.Row,
alignItems: ItemAlign.Center,
justifyContent: FlexAlign.Center,
wrap: FlexWrap.Wrap
}) {
ForEach(this.imageList, (item: NavItem) => {
Column() {
Image(item.icon)
.width(80)
.height(80)
Text(item.title)
}
.margin(10)
})
}
.backgroundColor('#FFFFFF')
.padding(20)
.width(this.rate * 100 + '%')
.borderRadius(16)
写在最后
有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)文档用来跟着学习是非常有必要的。
这份鸿蒙(HarmonyOS NEXT)文档包含了鸿蒙开发必掌握的核心知识要点,内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。
希望这一份鸿蒙学习文档能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!
获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习文档
鸿蒙(HarmonyOS NEXT)5.0最新学习路线
有了路线图,怎么能没有学习文档呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。
获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习文档
《鸿蒙 (OpenHarmony)开发入门教学视频》
《鸿蒙生态应用开发V3.0白皮书》
《鸿蒙 (OpenHarmony)开发基础到实战手册》
OpenHarmony北向、南向开发环境搭建
《鸿蒙开发基础》
●ArkTS语言
●安装DevEco Studio
●运用你的第一个ArkTS应用
●ArkUI声明式UI开发
.……
《鸿蒙开发进阶》
●Stage模型入门
●网络管理
●数据管理
●电话服务
●分布式应用开发
●通知与窗口管理
●多媒体技术
●安全技能
●任务管理
●WebGL
●国际化开发
●应用测试
●DFX面向未来设计
●鸿蒙系统移植和裁剪定制
……
《鸿蒙进阶实战》
●ArkTS实践
●UIAbility应用
●网络案例
……