柵格容器組件

2024-01-22 17:51 更新

柵格容器組件,僅可以和柵格子組件(GridCol)在柵格布局場(chǎng)景中使用。

說(shuō)明

該組件從API Version 9開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

子組件

可以包含GridCol子組件。

接口

GridRow(option?: {columns?: number | GridRowColumnOption, gutter?: Length | GutterOption, breakpoints?: BreakPoints, direction?: GridRowDirection})

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

gutter

Length | GutterOption

柵格布局間距,x代表水平方向。

columns

number | GridRowColumnOption

設(shè)置布局列數(shù)。

breakpoints

BreakPoints

設(shè)置斷點(diǎn)值的斷點(diǎn)數(shù)列以及基于窗口或容器尺寸的相應(yīng)參照。

direction

GridRowDirection

柵格布局排列方向。

GutterOption

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

x

Length | GridRowSizeOption

水平gutter option。

y

Length | GridRowSizeOption

豎直gutter option。

GridRowColumnOption

柵格在不同寬度設(shè)備類型下,柵格列數(shù)。

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

xs

number

最小寬度類型設(shè)備。

sm

number

小寬度類型設(shè)備。

md

number

中等寬度類型設(shè)備。

lg

number

大寬度類型設(shè)備。

xl

number

特大寬度類型設(shè)備。

xxl

number

超大寬度類型設(shè)備。

GridRowSizeOption

柵格在不同寬度設(shè)備類型下,gutter的大小。

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

xs

Length

最小寬度類型設(shè)備。

sm

Length

小寬度類型設(shè)備。

md

Length

中等寬度類型設(shè)備。

lg

Length

大寬度類型設(shè)備。

xl

Length

特大寬度類型設(shè)備。

xxl

Length

超大寬度類型設(shè)備。

BreakPoints

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

value

Array<string>

設(shè)置斷點(diǎn)位置的單調(diào)遞增數(shù)組。

默認(rèn)值:["320vp", "520vp", "840vp"]

reference

BreakpointsReference

斷點(diǎn)切換參照物。

  1. // 啟用xs、sm、md共3個(gè)斷點(diǎn)
  2. breakpoints: {value: ["100vp", "200vp"]}
  3. // 啟用xs、sm、md、lg共4個(gè)斷點(diǎn),斷點(diǎn)范圍值必須單調(diào)遞增
  4. breakpoints: {value: ["320vp", "520vp", "840vp"]}
  5. // 啟用xs、sm、md、lg、xl共5個(gè)斷點(diǎn),斷點(diǎn)范圍數(shù)量不可超過(guò)斷點(diǎn)可取值數(shù)量-1
  6. breakpoints: {value: ["320vp", "520vp", "840vp", "1080vp"]}

BreakpointsReference枚舉類型

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

枚舉名

描述

WindowSize

以窗口為參照。

ComponentSize

以容器為參照。

GridRowDirection枚舉類型

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

枚舉名

描述

Row

柵格元素按照行方向排列。

RowReverse

柵格元素按照逆序行方向排列。

柵格最多支持xs、sm、md、lg、xl、xxl六個(gè)斷點(diǎn),且名稱不可修改。假設(shè)傳入的數(shù)組是[n0, n1, n2, n3, n4],各個(gè)斷點(diǎn)取值如下:

斷點(diǎn)

取值范圍

xs

[0, n0)

sm

[n0, n1)

md

[n1, n2)

lg

[n2, n3)

xl

[n3, n4)

xxl

[n4, INF)

說(shuō)明:

  • 柵格元素僅支持Row/RowReverse排列,不支持column/ColumnReverse方向排列。
  • 柵格子組件僅能通過(guò)span、offset計(jì)算子組件位置與大小。多個(gè)子組件span超過(guò)規(guī)定列數(shù)時(shí)自動(dòng)換行。
  • 單個(gè)元素span大小超過(guò)最大列數(shù)時(shí)后臺(tái)默認(rèn)span為最大column數(shù)。
  • 新一行的Offset加上子組件的span超過(guò)總列數(shù)時(shí),將下一個(gè)子組件在新的一行放置。
  • 例:Item1: GridCol({ span: 6}), Item2: GridCol({ span: 8, offset:11})

1

2

3

4

5

6

7

8

9

10

11

12

°

°

°

°

°

°

-

-

-

-

-

-

-

-

-

-

-

       

°

°

°

°

°

°

°

°

    

屬性

支持通用屬性。

事件

onBreakpointChange

onBreakpointChange(callback: (breakpoints: string) => void)

從API version 9開(kāi)始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

參數(shù)類型

必填

說(shuō)明

breakpoints

string

斷點(diǎn)發(fā)生變化時(shí)觸發(fā)回調(diào)

取值為"xs"、"sm"、"md"、"lg"、"xl"、"xxl"。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct GridRowExample {
  5. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown]
  6. @State currentBp: string = 'unknown'
  7. build() {
  8. Column() {
  9. GridRow({
  10. columns: 5,
  11. gutter: { x: 5, y: 10 },
  12. breakpoints: { value: ["400vp", "600vp", "800vp"],
  13. reference: BreakpointsReference.WindowSize },
  14. direction: GridRowDirection.Row
  15. }) {
  16. ForEach(this.bgColors, (color) => {
  17. GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }) {
  18. Row().width("100%").height("20vp")
  19. }.borderColor(color).borderWidth(2)
  20. })
  21. }.width("100%").height("100%")
  22. .onBreakpointChange((breakpoint) => {
  23. this.currentBp = breakpoint
  24. })
  25. }.width('80%').margin({ left: 10, top: 5, bottom: 5 }).height(200)
  26. .border({ color: '#880606', width: 2 })
  27. }
  28. }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)