• HarmonyOS列表组件


    List组件的使用

    1. import router from '@ohos.router'
    2. @Entry
    3. @Component
    4. struct Index {
    5. private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    6. build() {
    7. Row() {
    8. Column() {
    9. List({ space: 10 }) {
    10. ForEach(this.arr, (item: number) => {
    11. ListItem() {
    12. Text(`${item}`)
    13. .width('100%')
    14. .height(100)
    15. .fontSize(20)
    16. .fontColor(Color.White)
    17. .textAlign(TextAlign.Center)
    18. .borderRadius(10)
    19. .backgroundColor(0x007DFF)
    20. .onClick(() => {
    21. if (item === 0) {
    22. //跳转到GridPage页面
    23. router.push({
    24. url: 'pages/GridPage'
    25. })
    26. }
    27. })
    28. }
    29. }, item => item)
    30. }
    31. //strokeWidth: 分割线的线宽。
    32. //color: 分割线的颜色。
    33. //startMargin:分割线距离列表侧边起始端的距离。
    34. //endMargin: 分割线距离列表侧边结束端的距离。
    35. // .divider({
    36. // strokeWidth: 1,
    37. // color: Color.Gray,
    38. // startMargin: 10,
    39. // endMargin: 10
    40. // })
    41. //Vertical(默认值):子组件ListItem在List容器组件中呈纵向排列。
    42. //子组件ListItem在List容器组件中呈横向排列。
    43. .listDirection(Axis.Vertical)
    44. }
    45. .padding(12)
    46. .height('100%')
    47. .backgroundColor(0xF1F3F5)
    48. }
    49. .height('100%')
    50. }
    51. }

    List下用ForEach循环数据,列表子项用ListItem组件,组件中再设置布局。

    divider属性设置列表分割线,listDirection属性设置列表是横向排列还是纵向排列(默认纵向)。

    Grid组件的使用

    1. @Entry
    2. @Component
    3. struct GridPage {
    4. // 定义一个数组
    5. private arr: string[] = new Array(50)
    6. .fill('')
    7. .map((_, index) => `数据 ${index + 1}`);
    8. build() {
    9. Column() {
    10. Grid() {
    11. ForEach(this.arr, (item: string) => {
    12. GridItem() {
    13. Text(item)
    14. .fontSize(16)
    15. .fontColor(Color.White)
    16. .backgroundColor(0x007DFF)
    17. .width('100%')
    18. .height(80)
    19. .textAlign(TextAlign.Center)
    20. .borderRadius(10)
    21. }
    22. }, item => item)
    23. }
    24. //指定当前网格布局中的列数(数字代表列的权重)
    25. .columnsTemplate('2fr 3fr 2fr 3fr')
    26. //设置当前网格布局中的行数(数字代表行的权重)
    27. // .rowsTemplate('1fr 1fr 2fr 1fr')
    28. //设置列之间的间距
    29. .columnsGap(10)
    30. //设置行之间的距离
    31. .rowsGap(10)
    32. .height('100%')
    33. }
    34. .width('100%')
    35. .padding(10)
    36. }
    37. }

    GridItem设置子项,Grid下使用columnsTemplate属性指定当前网格布局中的列数。

    rowsTemplate属性设置行数,数字代表给每行或每列设置占比的权重。

    columnsGap属性设置列之间的间距,rowsGap属性设置行之间的距离。

    如果行和列数同时设置只会显示设置行列内的数据,其他数据不会显示,并且只能显示在一屏内,不支持滑动,所以一般行数和列数只设置一个。

    新建页面记得在config.json文件下的pages中添加,不然不会跳转。我这里创建项目用的API8,API9添加文件稍微有点差异。用最新API9的话运行到API8的远程设备会报错,所以这里创建项目选择了API和SDK都用了8.

  • 相关阅读:
    Python实现自动化网页操作
    基于ISO智能交通系统框架的 LTE-V2X技术规范
    李宏毅深度学习self-attentin学习笔记
    使用docker安装elasticsearch 7.4.2
    【数据结构初阶】顺序表
    CentOS 9 Stream 上安装 WebStorm
    ROS1云课→07基础概念
    一个依赖解决 Spring Boot 反爬虫,防止接口盗刷
    【Linux网络编程】Socket-TCP实例
    Kerberos (二) --------- Hadoop Kerberos 配置
  • 原文地址:https://blog.csdn.net/juer2017/article/details/134208048