可以通过获取最高子组件的高度并赋予其它子组件相应的高度,实现所有子组件高度和最高子组件一致:
@Entry @Component struct Index { @State subHeight: Length = 0; build() { Column() { Row() { Column() { Text('我是文本1'); Text('我是文本2'); } .backgroundColor('#ff0a59f7') .height(this.subHeight == 0 ? 'auto' : this.subHeight); Column() { Text('我是文本1'); Text('我是文本2'); Text('我是文本3'); } .backgroundColor('#ff96bcff') .height(this.subHeight == 0 ? 'auto' : this.subHeight); Column() { Text('我是文本1'); Text('我是文本2'); Text('我是文本3'); Text('我是文本4'); } .backgroundColor('#ffd1d1d6') .height(this.subHeight === 0 ? 'auto' : this.subHeight); } .backgroundColor('#ff5b5b5b') .onAreaChange((oldValue: Area, newValue: Area) => { this.subHeight = newValue.height; }); } .width('100%') .padding({ top: 20 }); } }
class TextClass { name: string[] = []; color: string = ''; index: number = 0; constructor(name: string[], color: string, index: number) { this.name = name; this.color = color; this.index = index; } } @Entry @Component struct Index2 { @State textList: TextClass[] = []; @State subHeight: Length = 0; aboutToAppear(): void { this.textList.push(new TextClass(['我是文本1', '我是文本2'], '#ff0a59f7', 0)); this.textList.push(new TextClass(['我是文本1', '我是文本2', '我是文本3'], '#ff96bcff', 1)); this.textList.push(new TextClass(['我是文本1', '我是文本2', '我是文本3', '我是文本4'], '#ffd1d1d6', 2)); } build() { Column() { Row() { ForEach(this.textList, (item: TextClass) => { Column() { ForEach(item.name, (text: string) => { Text(text) .fontSize('16fp'); }); } .backgroundColor(item.color) .height(this.subHeight === 0 ? 'auto' : this.subHeight); }); } .backgroundColor('#ff5b5b5b') .onAreaChange((oldValue: Area, newValue: Area) => { this.subHeight = newValue.height; }); } .width('100%') .padding({ top: 20 }); } }