網(wǎng)格容器中單項(xiàng)內(nèi)容容器

2024-01-22 17:52 更新

網(wǎng)格容器中單項(xiàng)內(nèi)容容器。

說明

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

子組件

可以包含單個子組件。

接口

GridItem()

屬性

名稱

參數(shù)類型

描述

rowStart

number

指定當(dāng)前元素起始行號。

rowEnd

number

指定當(dāng)前元素終點(diǎn)行號。

columnStart

number

指定當(dāng)前元素起始列號。

columnEnd

number

指定當(dāng)前元素終點(diǎn)列號。

forceRebuild(deprecated)

boolean

設(shè)置在觸發(fā)組件build時是否重新創(chuàng)建此節(jié)點(diǎn)。

從API version9開始廢棄。GridItem會根據(jù)自身屬性和子組件變化自行決定是否需要重新創(chuàng)建,無需設(shè)置。

默認(rèn)值:false

selectable8+

boolean

當(dāng)前GridItem元素是否可以被鼠標(biāo)框選。

說明:

> 外層Grid容器的鼠標(biāo)框選開啟時,GridItem的框選才生效。

默認(rèn)值:true

說明

起始行號、終點(diǎn)行號、起始列號、終點(diǎn)列號生效規(guī)則如下:

  • rowStart/rowEnd合理取值范圍為0~總行數(shù)-1,columnStart/columnEnd合理取值范圍為0~總列數(shù)-1。
  • 只有在設(shè)置columnTemplate和rowTemplate的Grid中,設(shè)置合理的rowStart/rowEnd/columnStart/columnEnd四個屬性的GridItem才能按照指定的行列號布局。
  • 在設(shè)置columnTemplate和rowTemplate的Grid中,單獨(dú)設(shè)置行號rowStart/rowEnd或列號columnStart/columnEnd的GridItem會占用指定的行數(shù)(rowEnd-rowStart+1)或列數(shù)(columnEnd-columnStart+1)。
  • 在只設(shè)置columnTemplate的Grid中設(shè)置列號columnStart/columnEnd的GridItem按照指定的列數(shù)布局。
  • 在只設(shè)置rowTemplate的Grid中設(shè)置行號rowStart/rowEnd的GridItem按照指定的行數(shù)布局。
  • columnTemplate和rowTemplate都不設(shè)置的Grid中GridItem的行列號屬性無效。

事件

名稱

功能描述

onSelect(event: (isSelected: boolean) => void)8+

GridItem元素被鼠標(biāo)框選的狀態(tài)改變時觸發(fā)回調(diào)。

isSelected:進(jìn)入鼠標(biāo)框選范圍即被選中返回true, 移出鼠標(biāo)框選范圍即未被選中返回false。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct GridItemExample {
  5. @State numbers: string[] = Array.apply(null, { length: 16 }).map(function (item, i) {
  6. return i.toString()
  7. })
  8. build() {
  9. Column() {
  10. Grid() {
  11. GridItem() {
  12. Text('4')
  13. .fontSize(16)
  14. .backgroundColor(0xFAEEE0)
  15. .width('100%')
  16. .height('100%')
  17. .textAlign(TextAlign.Center)
  18. }.rowStart(1).rowEnd(2).columnStart(1).columnEnd(2) // 同時設(shè)置合理的行列號
  19. ForEach(this.numbers, (item) => {
  20. GridItem() {
  21. Text(item)
  22. .fontSize(16)
  23. .backgroundColor(0xF9CF93)
  24. .width('100%')
  25. .height('100%')
  26. .textAlign(TextAlign.Center)
  27. }
  28. }, item => item)
  29. GridItem() {
  30. Text('5')
  31. .fontSize(16)
  32. .backgroundColor(0xDBD0C0)
  33. .width('100%')
  34. .height('100%')
  35. .textAlign(TextAlign.Center)
  36. }.columnStart(1).columnEnd(4) // 只設(shè)置列號,不會從第1列開始布局
  37. }
  38. .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
  39. .rowsTemplate('1fr 1fr 1fr 1fr 1fr')
  40. .width('90%').height(300)
  41. }.width('100%').margin({ top: 5 })
  42. }
  43. }

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號