9

弹性布局组件Flex—学习笔记之二-51CTO.COM

 2 years ago
source link: https://os.51cto.com/article/701839.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
弹性布局组件Flex—学习笔记之二-51CTO.COM
弹性布局组件Flex—学习笔记之二 原创
作者:木棉花潘颖琳 2022-02-17 20:07:45
Flex有五类参数,本篇讲justifyContent,alignItems和alignContent。

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

承接上篇文章,继续学习Flex组件(ง •_•)ง,同样以简单的Demo搭配效果图的形式去学习

Flex有五类参数,本篇讲justifyContent,alignItems和alignContent

25701e025f95aece87d403f6a1803efb28a868.png

1、JustifyContent的Demo

748f64757ce4913c45827525cf4531bb7f6ee6.pnga6bae9f9430801dd430631cb690c2427550788.png

a86b0dc061cbce5acbf17315f3802f397f9ecf.png

默认主轴方向direction为Row,即行排布,此demo的Flex容器组件内边距均为10

代码如下:

// Example 03
@Component
struct JustifyContentFlex {
  @Prop justifyContent : number
  @Prop text : string
  build() {
    Column({ space: 5 }) {
      Text(this.text).fontSize(15).width('90%')
      Flex({ justifyContent: this.justifyContent }) {
        Text('1').fontSize(20).width('20%').height(50).backgroundColor(0xF5DEB3)
        Text('2').fontSize(20).width('20%').height(50).backgroundColor(0xFFBC79)
        Text('3').fontSize(20).width('20%').height(50).backgroundColor(0xD2B48C)
      }
      .width('90%')
      .padding(10)
      .backgroundColor(0xAFEEEE)
    }
  }
}

@Entry
@Component
struct FlexExample3 {
  build() {
    Column({space:5}) {
        JustifyContentFlex({ text:'justifyContent: Start',justifyContent: FlexAlign.Start })
        JustifyContentFlex({ text:'justifyContent:Center',justifyContent: FlexAlign.Center })
        JustifyContentFlex({ text:'justifyContent:End',justifyContent: FlexAlign.End })
        JustifyContentFlex({ text:'justifyContent:SpaceBetween',justifyContent: FlexAlign.SpaceBetween })
        JustifyContentFlex({ text:'justifyContent:SpaceAround',justifyContent: FlexAlign.SpaceAround })
        JustifyContentFlex({ text:'justifyContent:SpaceEvenly',justifyContent: FlexAlign.SpaceEvenly })
    }.width('100%')
  }
}

2、AlignItems的Demo

89e012a7628e69f487a755efac2c7c91fef1e0.pnga334aa966685c0f1c29598d86dd7218d9b4c85.png

54a461d23287747084316861ae814a9e398fac.png

交叉轴为与主轴垂直的轴,若主轴为水平方向Row,则交叉轴为竖直方向Column;AlignItems的默认值Auto为Start;文本基线如下图所述,此Demo设置的文本大小均为20,可能效果图不是很明显,可以自己调整文本大小看看效果

e85ae9f15e06e8354d1159ca2bba3089c7e3ab.png

代码如下:

// Example 04
@Component
struct AlignItemsFlex {
  @Prop alignItems : number
  @Prop text : string
  build() {
    Column({ space: 5 }) {
      Text('alignItems:'+this.text).fontSize(15).width('90%')
      Flex({ alignItems: this.alignItems }) {
        Text('1').fontSize(20).width('33%').height(30).backgroundColor(0xF5DEB3)
        Text('2').fontSize(20).width('33%').height(40).backgroundColor(0xFFBC79)
        Text('3').fontSize(20).width('33%').height(50).backgroundColor(0xD2B48C)
      }
      .size({ width: '90%', height: 80 })
      .padding(10)
      .backgroundColor(0xAFEEEE)
    }.width('100%').margin({ top: 5 })
  }
}

@Entry
@Component
struct FlexExample4 {
  build() {
    Column() {
        AlignItemsFlex({ text:'Auto',alignItems: ItemAlign.Auto })
        AlignItemsFlex({ text:'Start',alignItems: ItemAlign.Start })
        AlignItemsFlex({ text:'Center',alignItems: ItemAlign.Center })
        AlignItemsFlex({ text:'End',alignItems: ItemAlign.End })
        AlignItemsFlex({ text:'Stretch',alignItems: ItemAlign.Stretch })
        AlignItemsFlex({ text:'Baseline',alignItems: ItemAlign.Baseline })
    }.width('100%')
  }
}

3、alignContent的Demo

17088a713bff0696f5e392129a2ed5f9efe586.png260ea62212e15d005f47309ddab485b8940112.png

a93aa5c32550ddffb79044575227058a774f88.png

32a1d3040e83227dcd849793f8e03c47fa417c.png

从效果图上看可知,这里是以行为元素的排布

代码如下:

// Example 05
@Component
struct AlignContentFlex {
  @Prop alignContent: number
  @Prop text : string
  build() {
    Column({ space: 5 }) {
      Text('alignContent:'+this.text).fontSize(15).width('90%')
      Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) {
        Text('1').fontSize(18).width('50%').height(20).backgroundColor(0xF5DEB3)
        Text('2').fontSize(18).width('50%').height(20).backgroundColor(0xFFBC79)
        Text('3').fontSize(18).width('50%').height(20).backgroundColor(0xD2B48C)
      }
      .size({ width: '90%', height: 90 })
      .padding(10)
      .backgroundColor(0xAFEEEE)
    }.width('100%').margin({ top: 5 })
  }
}

@Entry
@Component
struct FlexExample5 {
  build() {
    Column() {
        AlignContentFlex({ text:'Start',alignContent: FlexAlign.Start })
        AlignContentFlex({ text:'Center',alignContent: FlexAlign.Center })
        AlignContentFlex({ text:'End',alignContent: FlexAlign.End })
        AlignContentFlex({ text:'SpaceBetween',alignContent: FlexAlign.SpaceBetween })
        AlignContentFlex({ text:'SpaceAround',alignContent: FlexAlign.SpaceAround })
        AlignContentFlex({ text:'SpaceEvenly',alignContent: FlexAlign.SpaceEvenly })
    }.width('100%')
  }
}

以上就是我这次的小分享啦❀❀!!2022,学习路上继续前进!

​想了解更多内容,请访问:​

​51CTO和华为官方合作共建的鸿蒙技术社区​

​https://harmonyos.51cto.com​

责任编辑:jianghua 来源: 鸿蒙社区
zanpc.bd208a1.pngzanpchover.fdd60ba.png
weixin.23cd8b3.png 分享到微信
weibo.16d6b4f.png 分享到微博

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK