当前位置: 首页 > news >正文

【鸿蒙应用】Grid和GridItem组件

Grid是网格容器,有行和列组成,通过GridItem组件所在的单元格做出各种各样的布局。比如:

@Entry
@Preview
@Component
struct Index {private arr: string[] = ["1", "2", "31", "14", "51", "61", "71", "18"];build() {Column() {Grid() {ForEach(this.arr, (day: string) => {GridItem() {Text(day).fontSize(16).backgroundColor(0xF9CF93).width("100%").height("100%").textAlign(TextAlign.Center)}}, (day: string) => day)}.columnsTemplate("1fr 1fr 1fr 1fr 1fr").rowsTemplate("1fr 1fr 1fr 1fr 1fr").columnsGap(10).rowsGap(10).width("90%").backgroundColor(0xFAEEE0)}.height('100%').width('100%')}
}

通过columnsTemplate来设置当前网格布局列的数量,默认是 1 列。上面的代码效果如下图所示:

在这里插入图片描述

rowsTemplate属性,则是用来设置当前网格布局行的数量。

GridRow和GridCol

GridRow和GridCol只能在栅格布局场景中使用,GridRow常用的几个属性:

  • gutter:栅格布局的间距,x:表示水平方向
  • columns:设置布局列数
  • breakpoints:设置断点值的断点数列以及基于窗口或者容器尺寸的相应参照
  • direction:栅格布局的方向

实例代码:

@Entry
@Preview
@Component
struct Index {private bgColors: Color[] =[Color.Red, Color.Orange, Color.Black, Color.Yellow, Color.Green, Color.Pink, Color.Gray, Color.Blue, Color.Brown];build() {Column() {GridRow({columns: 5, // 设置布局列数gutter: { x: 5, y: 20 }, // 栅格布局的间距breakpoints: {value: ["400vp", "600vp", "800vp"],reference: BreakpointsReference.WindowSize},}) {ForEach(this.bgColors, (color:string) => {GridCol({ span: { xs: 1,sm:2,md:3,lg:4 } }){Row().width("100%").height("80vp")}.backgroundColor(color).borderWidth(2)})}}.height('100%').width('100%')}
}

在这里插入图片描述

这就是网格布局的相关组件


http://www.mrgr.cn/news/24613.html

相关文章:

  • [数据集][目标检测]汽车头部尾部检测数据集VOC+YOLO格式5319张3类别
  • 基于java+springboot+vue实现的林业产品推荐系统(文末源码+Lw)135
  • Python3网络爬虫开发实战(14)资讯类页面智能解析
  • 【一文看懂】Fanbox国内怎么支付?Fanbox PayPal付款失败?用下面的虚拟卡支付就可以了
  • 为AppInventor2开发自己的拓展(Extension) - 拓展开发入门篇
  • 免费云服务器申请教程
  • 安卓开发中LiveData的使用
  • openGauss增量备份与恢复技术详解及定时触发实现
  • DroidBot-GPT: GPT-powered UI Automation for Android论文学习
  • Vue组件:依赖注入provide和inject的使用
  • 代码随想录算法训练营day35
  • 架构理论碰撞:对比TOGAF、Zachman、DODAF和FEAF等主流架构框架
  • 龙蟠虎踞的搜索赛道,扛得住这场风云突变吗?
  • 【C++ Primer Plus习题】15.1
  • NoSQL之Redis
  • 文章在线改写如何效率高?智能改写神有妙招!
  • 【线程池】Java 线程池 ThreadPoolExecutor 类源码介绍
  • 深入理解 JavaScript:进阶概念与实战技巧
  • Linux用户和组群账户管理
  • 使用libmpeg解码mp3格式文件