Grid
表示网格布局,它可以设置行数和列数,它和 List
类似,子组件只能是 GridItem
。
interface GridInterface {
(scroller?: Scroller): GridAttribute;
}
Grid
的滚动。简单样例如下所示:
Grid() {
ForEach(this.columns, (item) => { // ForEach语法,循环创建GridItem
GridItem() { // 子组件只能是GirdItem
Text('Text: ' + item)
.fontSize(20)
.backgroundColor('#aabbcc')
.width('100%')
.height(70)
}
})
}
.columnsTemplate("1fr 1fr 1fr") // 设置Grid为3列,并且均分
.columnsGap(10) // 设置列间距为10vp
.rowsGap(10) // 设置行间距为10vp
.width('100%')
.height(170)
.backgroundColor(Color.Pink)
declare class GridAttribute<T> extends CommonMethod<T> {
columnsTemplate(value: string): T;
rowsTemplate(value: string): T;
columnsGap(value: number | string | Resource): T;
rowsGap(value: number | string | Resource): T;
cachedCount(value: number): T;
}
Grid
为 5 列,并把 Grid
宽度均分为 5 份,每列宽度占用 1 份。Grid
为 8 列,并把 Grid
宽度均分为 8 份,第一列占用 1 份,第二列占用 2 份,第三列占用 3 份,后两列每列占 1 份。columnsTemplate
一致。Grid
缓存数量。declare class GridAttribute<T> extends CommonMethod<T> {
onScrollIndex(event: (first: number) => void): T;
}
onScrollIndex
:当前列表显示的起始位置 item 发生变化时触发该回调。interface GridItemInterface {
(): GridItemAttribute;
}
GridItem
为无参定义
declare class GridItemAttribute<T> extends CommonMethod<T> {
rowStart(value: number): T;
rowEnd(value: number): T;
columnStart(value: number): T;
columnEnd(value: number): T;
forceRebuild(value: boolean): T;
}
简单样例如下所示:
Grid() {
ForEach(this.columns, (item, index) => {
GridItem() {
Text('Text: ' + item)
.fontSize(20)
.backgroundColor('#aabbcc')
.width('100%')
.height(70)
}
.columnStart(index == 0 ? 0 : 0) // 设置第一个Item布局从第一列开始
.columnEnd(index == 0 ? 2 : 0) // 设置第一个Item布局从第三列结束,也即是占满整行
})
}
.columnsTemplate("1fr 1fr 1fr") // 设置3列,每列均分
.columnsGap(10) // 设置列间距
.rowsGap(10) // 设置行间距
.width('100%')
.height(170)
.backgroundColor(Color.Pink)
样例中设置了 Grid
为 3 列,并且设置了第 1 个 GridItem
的列从 0 开始到 2 结束,所以 GridItem
就占满整行布局。
@Entry @Component struct ComponentTest {
private screenWidth: number = px2vp(1080); // 屏幕宽度
private rowSpace: number = 10; // 行间距
private rowCount: number = 5; // 行数
private columnSpace: number = 15; // 列间距
private columnCount: number = 4; // 列数
private itemSize: number = (this.screenWidth - (this.columnSpace * (this.columnCount + 1))) / this.columnCount; // Item尺寸
private items: number[] = [];
build() {
Column() {
Text()
.width('100%')
.height('100%')
.layoutWeight(1)
.backgroundColor(Color.Pink)
Grid() {
ForEach(this.items, (item, index) => { // ForEach语法
GridItem() {
Text('Text:' + index)
}
.width('100%')
.height('100%')
.backgroundColor('#bbccdd')
.rowStart(index == 15 ? 3 : 0) // 第16个GridItem占用2行
.rowEnd(index == 15 ? 4 : 0) // 第16个GridItem占用2行
.columnStart(index == 16 ? 0 : 0) // 第17个GridItem占用2列
.columnEnd((index == 16 ? 1 : 0)) // 第17个GridItem占用2列
})
}
.padding({left: this.columnSpace, right: this.columnSpace})
.columnsTemplate("1fr 1fr 1fr 1fr") // Grid宽度均分成4份
.rowsTemplate("1fr 1fr 1fr 1fr 1fr") // Grid高度均分成5份
.rowsGap(this.rowSpace) // 设置行间距
.columnsGap(this.columnSpace) // 设置列间距
.width('100%')
.height(this.itemSize * this.rowCount + this.rowSpace * (this.rowCount - 1))
}
.width('100%')
.height('100%')
}
private aboutToAppear() {
for(var i = 0; i < 18; i++) {
this.items[i] = i;
}
}
}
样例运行结果如下图所示:
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有