HarmonyOS中一个简单的自定义view

一个列表

  • 点击内容后置灰选项。
    在这里插入图片描述

自定义ItemView

  • 两个可选的传入参数,message、isComplete
@Component
export struct ItemView {
  @State message: string = 'Hello World';
  @State isComplete: boolean = false

  build() {

    Text(this.message)
      .width('100%')
      .backgroundColor('#d1c4e9')
      .padding(10)
      .margin(4)
      .opacity(this.isComplete ? 0.5 : 1)
      .decoration({
        type: this.isComplete ? TextDecorationType.LineThrough : TextDecorationType.None
      })
      .onClick(() => {
        this.isComplete = !this.isComplete
      })

  }
}

使用

  • 通过ForEach加载一个列表
import { ItemView } from './ItemView';

@Entry
@Component
struct Index {
  
  private list: Array<string> = ['内容1', '内容2', '内容3']

  build() {
    Column() {

      ForEach(this.list, (item: string) => {
        ItemView({ message: item })
      })

    }
    .height('100%')
    .width('100%')
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值