对于网格布局如何实现自定义的分割线?


HarmonyOS NEXT
2025-06-05 11:43:15
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
虎子船长

在griditem中添加自定义的分隔符。

参考如下demo:

@Entry
@Component
struct Indexdfgdgfh {
  @State data: string[][] = []
  @State colNum: number = 2
  getData() {
    this.data = []
    let arr: string[] = []
    for (let index = 0; index < 20; index++) {
      arr.push(`第${index}项`)
    }
    let arr2: string[] = []
    arr.forEach((item, index) => {
      arr2.push(item)
      if (index % this.colNum == this.colNum - 1) {
        this.data.push(arr2)
        arr2 = []
      }
    })
  }
  aboutToAppear(): void {
    this.getData()
  }
  build() {
    Column() {
      Column() {
        ForEach(this.data, (item: string[], strArrIndex) => {
          Row() {
            ForEach(item, (str: string, strIndex) => {
              Column() {
                Text(str)
              }
              .alignItems(HorizontalAlign.Center)
              .justifyContent(FlexAlign.Center)
              .layoutWeight(1)
              if (strIndex < item.length - 1) {
                Divider()
                  .vertical(true)
                  .padding({
                    top: 10,
                    bottom: 10
                  })
              }
            })
          }
          .width("100%")
          .height(40)
          .border({
            width: {
              bottom: strArrIndex + 1 === this.data.length ? 0 : 1
            }
          })
        })
      }
      .width("100%")
      .borderRadius(5)
      .backgroundColor(Color.Pink)
      .padding({
        left: 20,
        right: 20
      })
      Button('变成3列').onClick((event: ClickEvent) => {
        this.colNum = 3
        this.getData()
      })
    }
    .height("100%")
    .width("100%")
    .padding({
      left: '10%',
      right: '10%'
    })
  }
}
分享
微博
QQ
微信
回复
2025-06-06 14:28:16
相关问题
HarmonyOS 如何实现分割线拖拽?
755浏览 • 1回复 待解决
HarmonyOS Grid如何设置分割线
1036浏览 • 1回复 待解决
List如何设置分割线左右边距?
1311浏览 • 1回复 待解决
如何设置TabBar和TabContent分割线样式
3517浏览 • 1回复 待解决
Divider组件如何设置分割线宽度
4152浏览 • 1回复 待解决
如果让分割线垂直起来
6726浏览 • 1回复 待解决
【急】鸿蒙UI界面网格布局怎么设置?
18118浏览 • 4回复 待解决
鸿蒙怎么实现自定义布局Dialog
10503浏览 • 2回复 已解决