HarmonyOS 鸿蒙Next 自定义组件包含Divider,forEach循环渲染时Divider样式各异

HarmonyOS 鸿蒙Next 自定义组件包含Divider,forEach循环渲染时Divider样式各异 自定义组件包含Divider,forEach循环渲染这个自定义组件的时候Divider的样式每个组件不一样

代码如下

class item_we {
  img: string | Resource
  name: string
  message: string
  time: string

  constructor(img: string | Resource, name: string, message:string, time: string) {
    this.img = img
    this.name = name
    this.message = message
    this.time = time
  }
}

@Component
struct Wechat {

  weMessageArr: item_we[] = [
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14'),
    new item_we($r('app.media.app_icon'), '宝宝', '我爱你', '13:14')
  ]

  build() {
    Row() {
      Column() {
        header()
        List(){
          ForEach(this.weMessageArr,
            (item: item_we) => {
              ListItem() {
                List_item_we({ item: item })
              }
            })
        }
      }
      .height('100%')
      .width('100%')
    }
    .alignItems(VerticalAlign.Top)
    .height('100%')
  }
}

// 消息项
@Component
struct List_item_we {

  item: item_we
  build() {
    Column() {
      Divider().strokeWidth(1).color(Color.Red)
      Row({ space: 10 }) {
        Image(this.item.img)
          .height(40)
          .aspectRatio(1)
          .borderRadius(2)
        Column() {
          Text(this.item.name)
            .fontSize(18)
          Text(this.item.message)
            .fontSize(14)
            .fontColor(Color.Grey)
        }
        .height(40)
        .justifyContent(FlexAlign.SpaceAround)

        Blank()
        Text(this.item.time)
          .fontColor(Color.Grey)
          .textAlign(TextAlign.Center)

      }
      .width('100%')
      .padding({ left: 10, right: 10 })
      .height('90%')

    }
    .height(64)
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

// 头部
@Component
struct header {
  build() {
    Column() {
      Row({ space: 20}) {
        Text('微信')
          .fontSize(25)
          .margin({ left: 150})
        Blank()
        Image($r('app.media.ic_public_search'))
          .height(25)
          .aspectRatio(1)
        Image($r('app.media.ic_public_add_norm'))
          .height(25)
          .aspectRatio(1)
          .margin({ right: 20})
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
      .height(50)

    }
    .width('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next 自定义组件包含Divider,forEach循环渲染时Divider样式各异的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

预览器这种可能会出现显示的问题,用手机试的为主

更多关于HarmonyOS 鸿蒙Next 自定义组件包含Divider,forEach循环渲染时Divider样式各异的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


反复调试发现是预览器缩放的问题。

在HarmonyOS鸿蒙Next中,自定义组件使用forEach循环渲染时,Divider样式各异可能是因为在循环中为每个Divider设置了不同的样式属性或条件。Divider组件用于在UI中创建分隔线,其样式可以通过DividerStyleDividerAttribute进行自定义。如果在forEach循环中为每个Divider动态设置了不同的样式属性(如颜色、宽度、高度等),则会导致每个Divider的样式表现不同。

例如,在forEach循环中,可能根据数据源的某些条件为Divider设置了不同的colorstrokeWidth,导致每个Divider的样式不同。确保在循环中为Divider设置的样式属性一致,或者在循环外部统一设置Divider的默认样式,可以避免样式各异的情况。

回到顶部