零基础学鸿蒙编程鸿蒙开发入门ArkTS/ArkUI实战

15、鸿蒙/布局/弹性布局 (Flex)

2024-07-14  本文已影响0人  圆梦人生

概述

弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。常用于页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等。
容器默认存在主轴与交叉轴,子元素默认沿主轴排列,子元素在主轴方向的尺寸称为主轴尺寸,在交叉轴方向的尺寸称为交叉轴尺寸。

flex.png

基本概念

布局方向

在弹性布局中,容器的子元素可以按照任意方向排列。通过设置参数direction,可以决定主轴的方向,从而控制子元素的排列方向。

direction.png
@Component
export struct FlexLayout {
  build() {
    Column(){
      // 主轴为水平方向,子元素从起始端沿着水平方向开始排布
      Flex({direction: FlexDirection.Row}){
        Text("首页").backgroundColor(Color.Red).flexGrow(1)
        Text("新闻").backgroundColor(Color.Yellow).width(80)
        Text("汽车").backgroundColor(Color.Gray).flexGrow(1)
      }
    }
  }
}
@Component
export struct FlexLayout {
  build() {
    Column(){
      // 主轴为水平方向,子元素从终点端沿着FlexDirection. Row相反的方向开始排布
      Flex({direction: FlexDirection.RowReverse}){
        Text("首页").backgroundColor(Color.Red).flexGrow(1)
        Text("新闻").backgroundColor(Color.Yellow).width(80)
        Text("汽车").backgroundColor(Color.Gray).flexGrow(1)
      }
    }
  }
}
@Component
export struct FlexLayout {
  build() {
    Column(){
      // 主轴为垂直方向,子元素从起始端沿着垂直方向开始排布
      Flex({ direction: FlexDirection.Column }){
        Text("首页").backgroundColor(Color.Red).flexGrow(1)
        Text("新闻").backgroundColor(Color.Yellow).height(80)
        Text("汽车").backgroundColor(Color.Gray).flexGrow(1)
      }
    }
  }
}
@Component
export struct FlexLayout {
  build() {
    Column(){
      // 主轴为垂直方向,子元素从终点端沿着FlexDirection. Column相反的方向开始排布
      Flex({direction: FlexDirection.ColumnReverse}){
        Text("首页").backgroundColor(Color.Red).flexGrow(1)
        Text("新闻").backgroundColor(Color.Yellow).height(80)
        Text("汽车").backgroundColor(Color.Gray).flexGrow(1)
      }
    }
  }
}

布局换行

弹性布局分为单行布局和多行布局。默认情况下,Flex容器中的子元素都排在一条线(又称“轴线”)上。wrap属性控制当子元素主轴尺寸之和大于容器主轴尺寸时,Flex是单行布局还是多行布局。在多行布局时,通过交叉轴方向,确认新行排列方向。

// FlexWrap. NoWrap(默认值):不换行。如果子元素的宽度总和大于父元素的宽度,则子元素会被压缩宽度
Flex({wrap: FlexWrap.NoWrap}){
    Text("首页").backgroundColor(Color.Red).width('50%')
    Text("新闻").backgroundColor(Color.Yellow).width('50%')
    Text("汽车").backgroundColor(Color.Gray).width('50%')
}
// FlexWrap. Wrap:换行,每一行子元素按照主轴方向排列。
Flex({ wrap: FlexWrap.Wrap }){
  Text("首页").backgroundColor(Color.Red).width('50%')
  Text("新闻").backgroundColor(Color.Yellow).width('50%')
  Text("汽车").backgroundColor(Color.Gray).width('50%')
}
// FlexWrap. WrapReverse:换行,每一行子元素按照主轴反方向排列
Flex({wrap: FlexWrap.WrapReverse}){
  Text("首页").backgroundColor(Color.Red).width('50%')
  Text("新闻").backgroundColor(Color.Yellow).width('50%')
  Text("汽车").backgroundColor(Color.Gray).width('50%')
}

主轴对齐方式

通过justifyContent参数设置子元素在主轴方向的对齐方式。

justifyContent.png
//FlexAlign.Start(默认值):子元素在主轴方向起始端对齐, 第一个子元素与父元素边沿对齐,其他元素与前一个元素对齐。
Flex({ justifyContent: FlexAlign.Start }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}
// FlexAlign.Center:子元素在主轴方向居中对齐。
Flex({ justifyContent: FlexAlign.Center }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}
// FlexAlign.End:子元素在主轴方向终点端对齐, 最后一个子元素与父元素边沿对齐,其他元素与后一个元素对齐
Flex({ justifyContent: FlexAlign.End }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}
// FlexAlign.SpaceBetween:Flex主轴方向均匀分配弹性元素,相邻子元素之间距离相同。第一个子元素和最后一个子元素与父元素边沿对齐。
Flex({ justifyContent: FlexAlign.SpaceBetween }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}
// FlexAlign.SpaceAround:Flex主轴方向均匀分配弹性元素,相邻子元素之间距离相同。第一个子元素到主轴起始端的距离和最后一个子元素到主轴终点端的距离是相邻元素之间距离的一半。
Flex({ justifyContent: FlexAlign.SpaceAround }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}
 // FlexAlign.SpaceEvenly:Flex主轴方向元素等间距布局,相邻子元素之间的间距、第一个子元素与主轴起始端的间距、最后一个子元素到主轴终点端的间距均相等。
Flex({ justifyContent: FlexAlign.SpaceEvenly }){
  Text("首页").backgroundColor(Color.Red).width('10%')
  Text("新闻").backgroundColor(Color.Yellow).width('10%')
  Text("汽车").backgroundColor(Color.Gray).width('10%')
}

交叉轴对齐方式

容器和子元素都可以设置交叉轴对齐方式,且子元素设置的对齐方式优先级较高。

Flex({ alignItems: ItemAlign.Auto }){
        Text("首页").backgroundColor(Color.Red).width('10%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('10%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('10%').height(150)
}.backgroundColor(Color.Orange).height(220)
Flex({alignItems: ItemAlign.Start }){
        Text("首页").backgroundColor(Color.Red).width('10%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('10%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('10%').height(150)
}.backgroundColor(Color.Black).height(220)
Flex({alignItems: ItemAlign.Center }){
        Text("首页").backgroundColor(Color.Red).width('10%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('10%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('10%').height(150)
}.backgroundColor(Color.Pink).height(220)
Flex({alignItems: ItemAlign.End }){
        Text("首页").backgroundColor(Color.Red).width('10%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('10%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('10%').height(150)
}.backgroundColor(Color.Blue).height(220)
Flex({ alignItems: ItemAlign.Stretch }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('20%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(150)
}.backgroundColor(Color.Pink).height(220)
Flex({ alignItems: ItemAlign.Baseline }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(150)
        Text("新闻").backgroundColor(Color.Yellow).width('20%').height(150)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(150)
}.backgroundColor(Color.Brown).height(220)

子元素设置交叉轴对齐

子元素的alignSelf属性也可以设置子元素在父容器交叉轴的对齐格式,且会覆盖Flex布局容器中alignItems配置。下例中Flex容器中alignItems设置交叉轴子元素的对齐方式为居中,子元素自身设置了alignSelf属性的情况,覆盖父组件的alignItems值,表现为alignSelf的定义

Flex({direction: FlexDirection.Row, alignItems: ItemAlign.Center }){
  Text("首页").backgroundColor(Color.Red).width('20%').height(150)
          .alignSelf(ItemAlign.Start)
  Text("新闻").backgroundColor(Color.Yellow).width('20%').height(150)
          .alignSelf(ItemAlign.Baseline)
  Text("新闻").backgroundColor(Color.Green).width('20%').height(150)
          .alignSelf(ItemAlign.Center)
  Text("汽车").backgroundColor(Color.Gray).width('20%').height(150)
          .alignSelf(ItemAlign.End)
}.backgroundColor(Color.Pink).height(220)

内容对齐

Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Start, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.End, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.SpaceAround, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.SpaceEvenly, wrap: FlexWrap.Wrap }){
        Text("首页").backgroundColor(Color.Red).width('20%').height(50)
        Text("新闻").backgroundColor(Color.Yellow).width('40%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('50%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('70%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('30%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
        Text("汽车").backgroundColor(Color.Gray).width('20%').height(50)
}.backgroundColor(Color.Pink).height(300)

自适应拉伸

在弹性布局父组件尺寸过小时,通过子元素的以下属性设置其在父容器的占比,达到自适应布局。

Flex(){
  Text("首页").backgroundColor(Color.Red).height(50)
          .flexBasis('auto')
  Text("新闻").backgroundColor(Color.Yellow).height(50)
          .flexBasis(100)
  Text("汽车").backgroundColor(Color.Gray).height(50)
          .flexBasis(200)
}.backgroundColor(Color.Pink).height(300)
 Flex(){
        Text("首页").backgroundColor(Color.Red).height(50)
          .flexGrow(2)
        Text("新闻").backgroundColor(Color.Yellow).height(50)
          .flexGrow(1)
        Text("汽车").backgroundColor(Color.Gray).height(50)
          .width(100)
}.backgroundColor(Color.Pink).height(300)
Flex(){
  Text("首页").backgroundColor(Color.Red).height(50)
          .width(200)
          .flexShrink(3)
  Text("新闻").backgroundColor(Color.Yellow).height(50)
          .width(200)
  Text("汽车").backgroundColor(Color.Gray).height(50)
          .width(200)
          .flexShrink(2)
}.backgroundColor(Color.Pink).height(300)
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }){
          Text("首页").backgroundColor(Color.Red).height(50)
            .width(100)
          Text("新闻").backgroundColor(Color.Yellow).height(50)
            .width(100)
          Text("汽车").backgroundColor(Color.Gray).height(50)
            .width(100)
}.backgroundColor(Color.Pink).height(300)
上一篇 下一篇

猜你喜欢

热点阅读