HarmonyOS NEXT 自定义struct缩小异常?

阅读 522
1 个回答

你可以参考下面代码:

Column() { 
  Text('自定义struct缩小异常') 
  Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 
    SubView({ 
      title: '左边', 
      desc: this.leftContent, 
      color: Color.Green 
    }) 
 
    SubView({ 
      title: '右边', 
      desc: this.rightContent, 
      color: Color.Red 
    }) 
  } 
  .width(200).height(40) 
  .borderWidth(1) 
} 
 
//自定义struct: 
@Component 
export struct SubView { 
  @State title: string = '' 
  @State desc: string = '' 
  @State color: Color = Color.White 
 
  build() { 
    Row() { 
      //省略代码 
    } 
    .height('100%') 
    .flexShrink(1) 
    .backgroundColor(this.color) 
    .onClick(() => { 
      if (this.desc.length >= 10) { 
        this.desc = this.desc.slice(0, 2) 
      } else { 
        this.desc = `${this.desc}变长` 
      } 
    }) 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进