ForEach循环list中divider问题 HarmonyOS 鸿蒙Next

ForEach循环list中divider问题 HarmonyOS 鸿蒙Next 导致这些线条不统一是哪里的问题呢?

cke_2470.png


更多关于ForEach循环list中divider问题 HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

预览页不准的,用真机你会发现可能就没有这个问题了。

更多关于ForEach循环list中divider问题 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


预览器的问题,用远程模拟器显示是正常的

单位错了。不是数值型的。应该是字符串 如: .padding("15vp")

在HarmonyOS鸿蒙Next中,使用ForEach循环渲染列表时,如果需要在列表项之间添加分隔线(divider),可以通过在ForEachitemBuilder中返回包含分隔线的组件来实现。例如:

ForEach(
  this.listData,
  (item: string, index: number) => {
    return Column() {
      Text(item)
      if (index < this.listData.length - 1) {
        Divider().strokeWidth(1).color(Color.Gray)
      }
    }
  }
)

在上述代码中,Divider组件被添加到每个列表项的末尾,除了最后一个项。这样可以确保列表项之间有分隔线,但不会在最后一个项后添加多余的分隔线。Divider的样式可以通过strokeWidthcolor等属性进行自定义。

回到顶部