W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Flex(value?: { direction?: FlexDirection, wrap?: FlexWrap, justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })
標準Flex布局容器。詳細指導請參考彈性布局。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù)名 | 參數(shù)類型 | 必填 | 默認值 | 參數(shù)描述 |
---|---|---|---|---|
direction | 否 | FlexDirection.Row | 子組件在Flex容器上排列的方向,即主軸的方向。 | |
wrap | 否 | FlexWrap.NoWrap | Flex容器是單行/列還是多行/列排列。 說明: 在多行布局時,通過交叉軸方向,確認新行堆疊方向。 | |
justifyContent | 否 | FlexAlign.Start | 所有子組件在Flex容器主軸上的對齊格式。 | |
alignItems | 否 | ItemAlign.Start | 所有子組件在Flex容器交叉軸上的對齊格式。 | |
alignContent | 否 | FlexAlign.Start | 交叉軸中有額外的空間時,多行內(nèi)容的對齊方式。僅在wrap為Wrap或WrapReverse下生效。 |
- // xxx.ets
- @Entry
- @Component
- struct FlexExample1 {
- build() {
- Column() {
- Column({ space: 5 }) {
- Text('direction:Row').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ direction: FlexDirection.Row }) { // 子組件在容器主軸上行布局
- Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
- Text('4').width('20%').height(50).backgroundColor(0xD2B48C)
- }
- .height(70)
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- Text('direction:RowReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ direction: FlexDirection.RowReverse }) { // 子組件在容器主軸上反向行布局
- Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
- Text('4').width('20%').height(50).backgroundColor(0xD2B48C)
- }
- .height(70)
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- Text('direction:Column').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ direction: FlexDirection.Column }) { // 子組件在容器主軸上列布局
- Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)
- Text('2').width('100%').height(40).backgroundColor(0xD2B48C)
- Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)
- Text('4').width('100%').height(40).backgroundColor(0xD2B48C)
- }
- .height(160)
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- Text('direction:ColumnReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ direction: FlexDirection.ColumnReverse }) { // 子組件在容器主軸上反向列布局
- Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)
- Text('2').width('100%').height(40).backgroundColor(0xD2B48C)
- Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)
- Text('4').width('100%').height(40).backgroundColor(0xD2B48C)
- }
- .height(160)
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- }.width('100%').margin({ top: 5 })
- }.width('100%')
- }
- }
- // xxx.ets
- @Entry
- @Component
- struct FlexExample2 {
- build() {
- Column() {
- Column({ space: 5 }) {
- Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ wrap: FlexWrap.Wrap }) { // 子組件多行布局
- Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
- }
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ wrap: FlexWrap.NoWrap }) { // 子組件單行布局
- Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)
- }
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({ wrap: FlexWrap.WrapReverse , direction:FlexDirection.Row }) { // 子組件反向多行布局
- Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
- }
- .width('90%')
- .height(120)
- .padding(10)
- .backgroundColor(0xAFEEEE)
- }.width('100%').margin({ top: 5 })
- }.width('100%')
- }
- }
- // xxx.ets
- @Component
- struct JustifyContentFlex {
- justifyContent : number = 0;
- build() {
- Flex({ justifyContent: this.justifyContent }) {
- Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
- Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
- Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
- }
- .width('90%')
- .padding(10)
- .backgroundColor(0xAFEEEE)
- }
- }
- @Entry
- @Component
- struct FlexExample3 {
- build() {
- Column() {
- Column({ space: 5 }) {
- Text('justifyContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.Start }) // 子組件在容器主軸上首端對齊
- Text('justifyContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.Center }) // 子組件在容器主軸上居中對齊
- Text('justifyContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.End }) // 子組件在容器主軸上尾端對齊
- Text('justifyContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.SpaceBetween }) // 子組件在容器主軸上均分容器布局,第一個子組件與行首對齊,最后一個子組件與行尾對齊。
- Text('justifyContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.SpaceAround }) // 子組件在容器主軸上均分容器布局,第一個子組件到行首的距離和最后一個子組件到行尾的距離是相鄰子組件之間距離的一半。
- Text('justifyContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')
- JustifyContentFlex({ justifyContent: FlexAlign.SpaceEvenly }) // 子組件在容器主軸上均分容器布局,子組件之間的距離與第一子組件到行首、最后一個子組件到行尾的距離相等
- }.width('100%').margin({ top: 5 })
- }.width('100%')
- }
- }
- // xxx.ets
- @Component
- struct AlignItemsFlex {
- alignItems : number = 0
- build() {
- Flex({ alignItems: this.alignItems }) {
- Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)
- Text('2').width('33%').height(40).backgroundColor(0xD2B48C)
- Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
- }
- .size({width: '90%', height: 80})
- .padding(10)
- .backgroundColor(0xAFEEEE)
- }
- }
- @Entry
- @Component
- struct FlexExample4 {
- build() {
- Column() {
- Column({ space: 5 }) {
- Text('alignItems:Auto').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.Auto }) // 子組件在容器交叉軸上首部對齊
- Text('alignItems:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.Start }) // 子組件在容器交叉軸上首部對齊
- Text('alignItems:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.Center }) // 子組件在容器交叉軸上居中對齊
- Text('alignItems:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.End }) // 子組件在容器交叉軸上尾部對齊
- Text('alignItems:Stretch').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.Stretch }) // 子組件在容器交叉軸上拉伸填充
- Text('alignItems:Baseline').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignItemsFlex({ alignItems: ItemAlign.Baseline }) // 子組件在容器交叉軸上與文本基線對齊
- }.width('100%').margin({ top: 5 })
- }.width('100%')
- }
- }
- // xxx.ets
- @Component
- struct AlignContentFlex {
- alignContent: number
- build() {
- Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) {
- Text('1').width('50%').height(20).backgroundColor(0xF5DEB3)
- Text('2').width('50%').height(20).backgroundColor(0xD2B48C)
- Text('3').width('50%').height(20).backgroundColor(0xD2B48C)
- }
- .size({ width: '90%', height: 90 })
- .padding(10)
- .backgroundColor(0xAFEEEE)
- }
- }
- @Entry
- @Component
- struct FlexExample5 {
- build() {
- Column() {
- Column({ space: 5 }) {
- Text('alignContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignContentFlex({ alignContent: FlexAlign.Start }) // 多行布局下子組件首部對齊
- Text('alignContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignContentFlex({ alignContent: FlexAlign.Center }) // 多行布局下子組件居中對齊
- Text('alignContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignContentFlex({ alignContent: FlexAlign.End }) // 多行布局下子組件尾部對齊
- Text('alignContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignContentFlex({ alignContent: FlexAlign.SpaceBetween }) // 多行布局下第一行子組件與列首對齊,最后一行子組件與列尾對齊
- Text('alignContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')
- AlignContentFlex({ alignContent: FlexAlign.SpaceAround }) // 多行布局下第一行子組件到列首的距離和最后一行子組件到列尾的距離是相鄰行之間距離的一半
- Text('alignContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')
- Flex({
- wrap: FlexWrap.Wrap,
- alignContent: FlexAlign.SpaceEvenly
- }) { // 多行布局下相鄰行之間的距離與第一行子組件到列首的距離、最后一行子組件到列尾的距離完全一樣
- Text('1').width('50%').height(20).backgroundColor(0xF5DEB3)
- Text('2').width('50%').height(20).backgroundColor(0xD2B48C)
- Text('3').width('50%').height(20).backgroundColor(0xF5DEB3)
- Text('4').width('50%').height(20).backgroundColor(0xD2B48C)
- Text('5').width('50%').height(20).backgroundColor(0xF5DEB3)
- }
- .size({ width: '90%', height: 100 })
- .padding({ left: 10, right: 10 })
- .backgroundColor(0xAFEEEE)
- }.width('100%').margin({ top: 5 })
- }.width('100%')
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: