布局約束
通過組件的寬高比和顯示優(yōu)先級(jí)約束組件顯示效果。
說明:
開發(fā)前請(qǐng)熟悉鴻蒙開發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
屬性
名稱 | 參數(shù)說明 | 描述 |
---|---|---|
aspectRatio | number | 指定當(dāng)前組件的寬高比,aspectRatio = width/height。 從API version 9開始,該接口支持在ArkTS卡片中使用。 API version 9及以前,默認(rèn)值為:1.0。 API version 10:無默認(rèn)值。**說明:**該屬性在不設(shè)置值或者設(shè)置非法值時(shí)不生效。 例如,Row只設(shè)置寬度且沒有子組件,aspectRatio不設(shè)置值或者設(shè)置成負(fù)數(shù)時(shí),此時(shí)Row高度為0。 |
displayPriority | number | 設(shè)置當(dāng)前組件在布局容器中顯示的優(yōu)先級(jí),當(dāng)父容器空間不足時(shí),低優(yōu)先級(jí)的組件會(huì)被隱藏。 小數(shù)點(diǎn)后的數(shù)字不作優(yōu)先級(jí)區(qū)分,即區(qū)間為[x, x + 1)內(nèi)的數(shù)字視為相同優(yōu)先級(jí)。例如:1.0與1.9為同一優(yōu)先級(jí)。 從API version 9開始,該接口支持在ArkTS卡片中使用。**說明:**僅在Row/Column/Flex(單行)容器組件中生效。 |
示例
示例1
// xxx.ets
@Entry
@Component
struct AspectRatioExample {
private children: string[] = ['1', '2', '3', '4', '5', '6']
build() {
Column({ space: 20 }) {
Text('using container: row').fontSize(14).fontColor(0xCCCCCC).width('100%')
Row({ space: 10 }) {
ForEach(this.children, (item:string) = > {
// 組件寬度 = 組件高度*1.5 = 90
Text(item)
.backgroundColor(0xbbb2cb)
.fontSize(20)
.aspectRatio(1.5)
.height(60)
// 組件高度 = 組件寬度/1.5 = 60/1.5 = 40
Text(item)
.backgroundColor(0xbbb2cb)
.fontSize(20)
.aspectRatio(1.5)
.width(60)
}, (item:string) = > item)
}
.size({ width: "100%", height: 100 })
.backgroundColor(0xd2cab3)
.clip(true)
// grid子元素width/height=3/2
Text('using container: grid').fontSize(14).fontColor(0xCCCCCC).width('100%')
Grid() {
ForEach(this.children, (item:string) = > {
GridItem() {
Text(item)
.backgroundColor(0xbbb2cb)
.fontSize(40)
.width('100%')
.aspectRatio(1.5)
}
}, (item:string) = > item)
}
.columnsTemplate('1fr 1fr 1fr')
.columnsGap(10)
.rowsGap(10)
.size({ width: "100%", height: 165 })
.backgroundColor(0xd2cab3)
}.padding(10)
}
}
圖1 豎屏顯示
圖2 橫屏顯示
示例2
class ContainerInfo {
label: string = '';
size: string = '';
}
class ChildInfo {
text: string = '';
priority: number = 0;
}
@Entry
@Component
struct DisplayPriorityExample {
// 顯示容器大小
private container: ContainerInfo[] = [
{ label: 'Big container', size: '90%' },
{ label: 'Middle container', size: '50%' },
{ label: 'Small container', size: '30%' }
]
private children: ChildInfo[] = [
{ text: '1n(priority:2)', priority: 2 },
{ text: '2n(priority:1)', priority: 1 },
{ text: '3n(priority:3)', priority: 3 },
{ text: '4n(priority:1)', priority: 1 },
{ text: '5n(priority:2)', priority: 2 }
]
@State currentIndex: number = 0;
build() {
Column({ space: 10 }) {
// 切換父級(jí)容器大小
Button(this.container[this.currentIndex].label).backgroundColor(0x317aff)
.onClick(() = > {
this.currentIndex = (this.currentIndex + 1) % this.container.length;
})
// 通過變量設(shè)置Flex父容器寬度
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
ForEach(this.children, (item:ChildInfo) = > {
// 使用displayPriority給子組件綁定顯示優(yōu)先級(jí)
Text(item.text)
.width(120)
.height(60)
.fontSize(24)
.textAlign(TextAlign.Center)
.backgroundColor(0xbbb2cb)
.displayPriority(item.priority)
}, (item:ChildInfo) = > item.text)
}
.width(this.container[this.currentIndex].size)
.backgroundColor(0xd2cab3)
}.width("100%").margin({ top: 50 })
}
}
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`
橫屏顯示
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
59文章
2497瀏覽量
43742
發(fā)布評(píng)論請(qǐng)先 登錄
鴻蒙語言ArkTS(更好的生產(chǎn)力與性能)
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-ArkTS的聲明式開發(fā)范式
鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【按鍵事件】

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【Flex布局】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【背景設(shè)置】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【顯隱控制】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【形狀裁剪】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【柵格設(shè)置】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【菜單控制】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【組件標(biāo)識(shí)】 通用屬性

鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表【多態(tài)樣式】 通用屬性

評(píng)論