当前位置:   article > 正文

HarmonyOS应用开发学习-ArkUI-容器组件

HarmonyOS应用开发学习-ArkUI-容器组件

ArkUI-容器组件

1 ROW组件

沿水平方向布局容器。可以包含子组件

Row(value?:{space?:string | number})

参数:

参数名

参数类型

必填

默认值

参数描述

space

string | number

0

横向布局元素间距

属性:

名称

参数类型

默认值

描述

alignItems

VerticalAlign

VerticalAlign.Center

在垂直方向上子组件的对齐格式,

justifycontent

FlexAlign

FlexAlign.Start

设置子组件在水平方向上的对齐格式。

示例:

  1. //@Entry装饰器 入口组件
  2. @Entry
  3. //@Component装饰器 自定义组件(一个一个独立的模块)
  4. @Component
  5. struct Index { //自定义组件 Index组件的名称
  6. @State title: string = "你好!";
  7. build(){
  8. //build中的row组件只能写一个,多个写在row内部
  9. // {space:10} 子组件的间距
  10. Row({space:10}){
  11. //添加其他组件
  12. // Button('1')
  13. // Button('2')
  14. // Button('3')
  15. // 嵌套Row()
  16. Row()
  17. .width('30%')
  18. .height(50)
  19. .backgroundColor('red')
  20. Row()
  21. .width('30%')
  22. .height(50)
  23. .backgroundColor('green')
  24. Row()
  25. .width('30%')
  26. .height(50)
  27. .backgroundColor('blue')
  28. }
  29. .width('100%')
  30. .height(200)
  31. .border({width:2})
  32. .alignItems(VerticalAlign.Top)
  33. //垂直方向的对齐格式VerticalAlign.top /VerticalAlign.center /VerticalAlign.bottom
  34. .justifyContent(FlexAlign.SpaceEvenly)
  35. //水平方向的对齐方式
  36. // FlexAlign.Start FlexAlign.center FlexAlign.End 左中右
  37. //FlexAlign.SpaceBetween 两端对齐
  38. //FlexAlign.SpaceAround 左右间距相同
  39. //FlexAlign.SpaceEvenly 间隔相同
  40. }
  41. }

2 Column组件

沿垂直方向布局的容器。可以包含子组件。

Column(value?:{space?:string | number})

参数:

参数名

参数类型

必填

默认值

参数描述

space

string | number

0

纵向布局元素间距

属性:

名称

参数类型

默认值

描述

alignltems

HorizontalAlign

VerticalAlign.Center

设置子组件在水平方向上的对齐格式

justifyContent8+

FlexAlign

FlexAlign.Start

设置子组件在垂直方向上的对齐格式

示例:

  1. @Entry //@Entry装饰器 入口组件
  2. @Component ///@Component装饰器 自定义组件
  3. struct Index { //自定义组件 Index组件的名称
  4. @State title: string = "hello world";
  5. //UI描述
  6. build(){
  7. Column({space:10}){//space:10 设置子组件的间距
  8. //添加子组件
  9. Button('click1')
  10. Button('click2')
  11. Button('click3')
  12. }
  13. .width('100%')
  14. .height(200)
  15. .border({width:2})
  16. // 添加对齐方式(水平方向对齐)
  17. // 左对齐
  18. .alignItems(HorizontalAlign.Start)
  19. // 居中对齐(默认值)
  20. // .alignItems(HorizontalAlign.Center)
  21. // 右对齐
  22. // .alignItems(HorizontalAlign.End)
  23. //添加对齐方式-垂直方向对齐
  24. // 上对齐
  25. .justifyContent(FlexAlign.Start)
  26. // 居中对齐
  27. .justifyContent(FlexAlign.Center)
  28. // 下对齐
  29. .justifyContent(FlexAlign.End)
  30. // 两端对齐
  31. .justifyContent(FlexAlign.SpaceBetween)
  32. // 元素左右外边距相等
  33. .justifyContent(FlexAlign.SpaceAround)
  34. // 元素所有间距相同
  35. .justifyContent(FlexAlign.SpaceEvenly)
  36. }
  37. }

3 Flex组件

以弹性方式布局子组件的容器组件。

子组件 可以包含子组件。

  1. 接口
  2. Flex(value?:{ direction?: FlexDirection, wrap?: Flexwrap, justifycontent?.FlexAlign,alignItems?: ItemAlign,aligncontent?: FlexAlign })

参数:

参数名

参数类型

必填

默认值

参数描述

direction

FlexDirection

FlexDirection.Row

子组件在Flex容器上排列的方向,即主轴的方向。

wrap

FlexWrap

FlexWrap.NoWrap

Flex容器是单行/列还是多行列排列。

justifyContent

FlexAlign

FlexAlign.Start

子组件在Flex容器主轴上的对齐格式。

alignltems

ItemAlign

ltemAlign.Stretch

子组件在Flex容器交叉轴上的对齐格式。

alignContent

FlexAlign

FlexAlign.Start

交叉轴中有额外的空间时多行内容的对齐方式。仅在wrap为Wrap或WrapReverse下生效

3.1 direction

设置子组件在flex容器中的排列方式

语法:

  1. Flex({direction:FlexDirection.属性值}){
  2. }

属性值:

Row:水平方向排列

Column:垂直方向排列

RowReverse:水平方向逆向排列

ColumnReverse:垂直方向逆向排列

示例:

  1. @Entry //@Entry装饰器 入口组件
  2. @Component ///@Component装饰器 自定义组件
  3. struct Index { //自定义组件 Index组件的名称
  4. @State title: string = "hello world";
  5. //UI描述
  6. build(){
  7. Column({space:10}) {
  8. //子组件在Flex容器上排列的方向,即主轴的方向
  9. // direction
  10. // direction:FlexDirection.Row 横向排列
  11. // direction:FlexDirection.Column 纵向排列
  12. Flex({direction:FlexDirection.Row}){
  13. Text('1')
  14. .width('20%')
  15. .height(50)
  16. .backgroundColor('red')
  17. Text('2')
  18. .width('20%')
  19. .height(50)
  20. .backgroundColor(0x904523)
  21. Text('3')
  22. .width('20%')
  23. .height(50)
  24. .backgroundColor('green')
  25. Text('4')
  26. .width('20%')
  27. .height(50)
  28. .backgroundColor(0x123458)
  29. }
  30. .width('90%')
  31. .height(100)
  32. // 单词
  33. .backgroundColor('red')
  34. // #16进制
  35. .backgroundColor('#F00')
  36. // 0X + 16进制
  37. .backgroundColor(0X0F0)
  38. }
  39. .width('100%')
  40. .margin({top:10})
  41. }
  42. }
3.2 wrap

设置子组件在flex容器中的排列方式

语法:

  1. Flex({wrap:FlexWrap.属性值}){
  2. }

属性值:

Warp:换行

NoWarp:不换行

WrapReverse 换行逆向

示例:

  1. @Entry //@Entry装饰器 入口组件
  2. @Component ///@Component装饰器 自定义组件
  3. struct Index { //自定义组件 Index组件的名称
  4. @State title: string = "hello world";
  5. //UI描述
  6. build(){
  7. Column({space:10}) {
  8. // warp 设置flex容器单行/多行排列
  9. // wrap:FlexWrap.Wrap 换行
  10. //wrap:FlexWrap.NoWrap 不换行
  11. // wrap:FlexWrap.WrapReverse 换行逆向
  12. Flex({wrap:FlexWrap.WrapReverse}){
  13. Text('1')
  14. .width('50%')
  15. .height(50)
  16. .backgroundColor('red')
  17. Text('2')
  18. .width('20%')
  19. .height(50)
  20. .backgroundColor(0x904523)
  21. Text('3')
  22. .width('20%')
  23. .height(50)
  24. .backgroundColor('green')
  25. Text('4')
  26. .width('20%')
  27. .height(50)
  28. .backgroundColor(0x123458)
  29. }
  30. .width('90%')
  31. .height(100)
  32. // 单词
  33. .backgroundColor('red')
  34. // #16进制
  35. .backgroundColor('#F00')
  36. // 0X + 16进制
  37. .backgroundColor(0X0F0)
  38. }
  39. .width('100%')
  40. .margin({top:10})
  41. }
  42. }
3.3 justifyContent

设置子组件在flex容器中的主轴的对齐方式

语法:

  1. Flex({justifyContent:FlexAlign.属性值}){
  2. }

属性值:

  • Start 顶部对齐
  • Center 居中对齐
  • End 尾部对齐
  • SpaceBetween 两端对齐
  • SpaceAround 左右外边距相同
  • SpaceEvenly 间距相同

示例:

  1. @Entry //@Entry装饰器 入口组件
  2. @Component ///@Component装饰器 自定义组件
  3. struct Index { //自定义组件 Index组件的名称
  4. @State title: string = "hello world";
  5. //UI描述
  6. build(){
  7. Column({space:10}) {
  8. // justifyContent 设置flex容器在主轴的对齐方式
  9. // justifyContent:FlexAlign.Start 顶部对齐
  10. // justifyContent:FlexAlign.Center 居中对齐
  11. // justifyContent:FlexAlign.End 尾部对齐
  12. // justifyContent:FlexAlign.SpaceBetween 两端对齐
  13. // justifyContent:FlexAlign.SpaceAround 左右外边距相同
  14. // justifyContent:FlexAlign.SpaceEvenly 间距相同
  15. Flex({justifyContent:FlexAlign.SpaceEvenly}){
  16. Text('1')
  17. .width('20%')
  18. .height(50)
  19. .backgroundColor('red')
  20. Text('2')
  21. .width('20%')
  22. .height(50)
  23. .backgroundColor(0x904523)
  24. Text('3')
  25. .width('20%')
  26. .height(50)
  27. .backgroundColor('green')
  28. Text('4')
  29. .width('20%')
  30. .height(50)
  31. .backgroundColor(0x123458)
  32. }
  33. .width('90%')
  34. .height(100)
  35. // 单词
  36. .backgroundColor('red')
  37. // #16进制
  38. .backgroundColor('#F00')
  39. // 0X + 16进制
  40. .backgroundColor(0X0F0)
  41. }
  42. .width('100%')
  43. .margin({top:10})
  44. }
  45. }
3.4 alignltems

设置子组件在flex容器中的交叉轴的对齐方式

语法:

  1. Flex({alignItems:ItemAlign.属性值}){
  2. }

属性值:

  • Start 顶部对齐
  • Center 居中对齐
  • End 底部对齐
  • Stretch 伸展,拉伸到容器尺寸

示例:

  1. @Entry //@Entry装饰器 入口组件
  2. @Component ///@Component装饰器 自定义组件
  3. struct Index { //自定义组件 Index组件的名称
  4. @State title: string = "hello world";
  5. //UI描述
  6. build(){
  7. Column({space:10}) {
  8. // alignltems 设置子组件在flex容器中的交叉轴的对齐方式
  9. // alignItems:ItemAlign.Start 顶部对齐
  10. // alignItems:ItemAlign.Center 居中对齐
  11. // alignItems:ItemAlign.End 底部对齐
  12. // alignItems:ItemAlign.Stretch 伸展,拉伸到容器尺寸
  13. Flex({alignItems:ItemAlign.Stretch}){
  14. Text('1')
  15. .width('20%')
  16. .height(30)
  17. .backgroundColor('red')
  18. Text('2')
  19. .width('20%')
  20. .height(40)
  21. .backgroundColor(0x904523)
  22. Text('3')
  23. .width('20%')
  24. .height(50)
  25. .backgroundColor('green')
  26. }
  27. .width('90%')
  28. .height(100)
  29. // 单词
  30. .backgroundColor('red')
  31. // #16进制
  32. .backgroundColor('#F00')
  33. // 0X + 16进制
  34. .backgroundColor(0X0F0)
  35. }
  36. .width('100%')
  37. .margin({top:10})
  38. }
  39. }

4 List

列表包含一系列相同宽度的列表项。适合连续、多行呈现同类数据,例如图片和文本。

  1. 子组件
  2. 包含ListItem子组件
  1. 接口
  2. List(value?:{space?: number | string, initialIndex?: number, scroller?:Scro11er})

5 ListItem

6 Tabs

通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。

  1. 子组件
  2. 包含子组件Tabcontent。

接口说明

Tabs(value?: {barPosition?: BarPosition, index?: number, controller?:Tabscontro1ler})

参数:

参数名

参数类型

必填

默认值

参数描述

barPosition

BarPosition

BarPosition.Start

指定页签位置来创建Tabs容器组件。

index

number

0

指定初次初始页签索引。

controller

TabsController

-

设置Tabs控制器。

BarPosition枚举说明

名称

描述

Start

vertical属性方法设置为true时,页签位于容器左侧;vertical属性方法设置为false时,页签位于容器顶部。

End

vertical属性方法设置为true时,页签位于容器右侧;vertical属性方法设置为false时,页签位于容器底部。

属性

不支持触摸热区设置。

名称

参数类型

默认值

描述

vertical

boolean

false

设置为false是为横向Tabs,设置为true时为纵向Tabs。

scrollable

BarMode

true

设置为true时可以通过滑动页面进行页面切换,为false时不可滑动切换页面。

barMode

BarMode

BarMode.Fixed

TabBar布局模式,具体描述见BarMode枚举说明。

barWidth

Length

-

TabBar的宽度值。

barHeight

Length

-

TabBar的高度值。

animationDuration

number

200

TabContent滑动动画时长,

BarMode枚举说明

名称

描述

Scrollable

TabBar使用实际布局宽度,超过总长度后可滑动。

Fixed

所有TabBar平均分配宽度。

事件

名称

描述

onChange(event: (index: number)=>

void)

Tab页签切换后触发的事件。-index:tab标签的索引值。

TabsController

Tabs组件的控制器,用于控制Tabs组件进行页签切换。

导入对象

controller:TabsController = new TabsController()

changeindex

控制Tabs切换到指定页签

changelndex(value: number): void

7 TabContent

仅在Tabs中使用,对应一个切换页签的内容视图。

  1. 接口
  2. Tabcontent()

属性

名称

参数类型

描述

tabBar

string | Resource | {icon?:string | Resource,text?:string | Resource} | CustomBuilder8+

设置TabBar上显示内容。CustomBuilder:构造器,内部可以传入组件(API8版本以上适用)。说明:如果icon采用svg格式图源,则要求svg图源删除其自有宽高属性值。如采用带有自有宽高属性的svg图源,icon大小则是svg本身内置的宽高属性值大小。

8 Swiper

滑块视图容器,提供子组件滑动轮播显示的能力。

  1. 子组件
  2. 可以包含子组件。
  1. 接口
  2. Swiper(controller?:SwiperController)

参数

参数名

参数类型

必墳

参数描述

controller

SwiperController

给组件绑定一个控制器,用来控制组件翻页

属性

名称

参数类型

描述

index

number

设置当前在容器中显示的子组件的索引值。默认值:0

autoPlay

boolean

子组件是否自动播放,自动播放状态下导航点不可操作。默认值:false

interval

number

使用自动播放时播放的时间间隔,单位为毫秒。默认值:3000

indicator

boolean

是否启用导航点指示器。默认值:true

loop

boolean

是否开启循环。设置为true时表示开启循环,在LazyForEach懒循环加载模式下加载的组件数量建议大于5个。默认值:true

duration

number

子组件切换的动画时长,单位为毫秒。默认值:400

vertical

boolean

是否为纵向滑动。默认值:false

itemSpace

number string

设置子组件与子组件之间间隙。默认值:0

displayMode

SwiperDisplayMode

设置子组件显示模式。默认值:SwiperDisplayMode.Stretch

cachedCount8+

number

设置预加载子组件个数。默认值:1

disableSwipe8+

boolean

禁用组件滑动切换功能,默认值:false

displayCount8+

number|string

设置一页中显示子组件的个数,设置为“auto”时等同于

SwiperDisplayMode.AutoLinear的显示效果。默认值:1

effectMode8+

EdgeEffect

设置滑动到边缘时的显示效果。默认值:EdgeEffect.Spring

curve8+

Curve|string

设置Swiper的动画曲线,默认为淡入淡出曲线

indicatorStyle8+

{left?: Length,top?:Length,right?:Length,bottom?: Length,size?:Length,mask?: boolean,color?:ResourceColor,selectedColor?:ResourceColor}

设置导航点样式:-left: 设置导航点距离Swiper组件左边的距离。- top: 设置导航点距离Swiper组件顶部的距离。-right: 设置导航点距离Swiper组件右边的距离。-bottom:设置导航点距离Swiper组件底部的距离。-size:设置导航点的直径。-mask: 设置是否显示导航点蒙层样式。一color: 设置导航点的颜色。-selectedcolor:设置选中的导航点的颜色

9 Grid

网格容器,由“行”和“列”分割的单元格所组成,通过指定”项目”所在的单元格做出各种各样的布局。

  1. 子组件
  2. 包含GridItem子组件。
  3. 接口
  4. Grid(scroller?:scroller)

参数:

参数名

参数类型

必填

参数描述

scroller

Scroller

可滚动组件的控制器,用于与可滚动组件进行绑定

属性

名称

参数类型

描述

columnsTemplate

string

设置当前网格布局列的数量,不设置时默认1列。例如,"1fr 1fr 2fr'是将父组件分3列,将父组件允许的宽分为4等份,第一列占1份,第二列占1份,第三列占2份。默认值:'1fr'

rowsTemplate

string

设置当前网格布局行的数量,不设置时默认1行。例如,"1fr 1fr 2fr”是将父组件分三行,将父组件允许的高分为4等份,第一行占1份,第二行占一份,第三行占2份。默认值:'1fr

columnsGap

Length

设置列与列的间距。默认值:0

rowsGap

Length

设置行与行的间距,默认值:0

scrollBar

BarState

设置滚动条状态。默认值:BarState.0ff

scrollBarColor

string |

number |

Color

设置滚动条的颜色。

scrollBarWidth

string |

number

设置滚动条的宽度。

cachedCount

number

设置预加载的Gridltem的数量,具体使用可参考减少应用白块说明。默认值:1

editMode8+

boolean

是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部Gridltem。默认值:false

10 GridItem

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/998883
推荐阅读
相关标签
  

闽ICP备14008679号