参考代码: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}变长` } }) } }
参考代码: