在Row布局中,左边是固定宽高组件,右边是Text组件的父组件,并且要求父组件占据Row组件剩余空间,但是当Text组件文字过多时会超出父组件设定范围,如何实现Text组件在父组件设定范围内的宽高自适应,避免超出父组件的设定范围?
问题效果预览:

aboutToAppear(): void { try { let screenW = this.getUIContext().px2vp(display.getDefaultDisplaySync().width); // 获取屏幕宽度,并转换为vp let stableW = this.getUIContext() .px2vp(this.uiContext?.getHostContext()?.resourceManager.getNumber($r('app.float.page_text_font_size').id)); // 获取页面字体大小,并转换为vp let marginR = Number(this.removeCharacter(this.marginRight as string)); let marginL = this.marginLeft as number; // 左边距直接转换为数字 let marginT = this.getUIContext() .px2vp(this.uiContext?.getHostContext()?.resourceManager.getNumber((this.marginTop as Resource).id)); // 顶部边距直接转换为vp this.maxWidth = screenW - stableW - marginR - marginL - marginT; // 计算最大宽度 console.info(`this.maxWidth: ${this.maxWidth}`); // 打印最大宽度 } catch (error) { } }
Row() { Text(this.str).constraintSize({ maxWidth: this.maxWidth }) .onClick(() => { this.str = this.str + ''; }); }
import { display } from '@kit.ArkUI'; @Entry @Component struct Index { @State str: string = '123'; uiContext = this.getUIContext(); @State maxWidth: number = 0; marginRight: Length = '16vp'; marginLeft: Length = 16; marginTop: Length = $r('app.float.page_text_font_size'); removeCharacter(str: string) { let result = str.replace(/[a-zA-Z]/g, ''); return result; } aboutToAppear(): void { try { let screenW = this.getUIContext().px2vp(display.getDefaultDisplaySync().width); // 获取屏幕宽度,并转换为vp let stableW = this.getUIContext() .px2vp(this.uiContext?.getHostContext()?.resourceManager.getNumber($r('app.float.page_text_font_size').id)); // 获取页面字体大小,并转换为vp let marginR = Number(this.removeCharacter(this.marginRight as string)); let marginL = this.marginLeft as number; // 左边距直接转换为数字 let marginT = this.getUIContext() .px2vp(this.uiContext?.getHostContext()?.resourceManager.getNumber((this.marginTop as Resource).id)); // 顶部边距直接转换为vp this.maxWidth = screenW - stableW - marginR - marginL - marginT; // 计算最大宽度 console.info(`this.maxWidth: ${this.maxWidth}`); // 打印最大宽度 } catch (error) { } } build() { Column() { TextInput({ text: $$this.str }); Row() { Row() { Row().width(50).height(50).backgroundColor('#ff18a2d0'); }; Row() { Text(this.str).constraintSize({ maxWidth: this.maxWidth }) .onClick(() => { this.str = this.str + ''; }); } .margin({ left: 10, right: 10 }); } .backgroundColor('#fff2f5f5'); } .width('100%') .height('100%') .backgroundColor('#ffeeeaea') .justifyContent(FlexAlign.Center); } }
Q:在解决方案的aboutToAppear方法中,使用getNumber方法获取其中的数据,得到的数据单位都是px吗?
A:用getNumber方法获取数据的数据单位都是px。例如Integer对应的是原数值,float中的数不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。具体参考官网资源管理。