折叠屏展开态打开应用和折叠态打开应用再展开,字体大小不一致。
- @State textFontSize: Resource | number | string = 15
- windowClass.on('windowSizeChange', (data) => {
- let uiContext: UIContext = windowClass.getUIContext()
- let widthBp: WidthBreakpoint = uiContext.getWindowWidthBreakpoint();
- AppStorage.setOrCreate('widthBp',widthBp)
- })
- // 三个值分别代表sm、md、lg下的值
- this.theFontSize = new BreakpointType('14fp', '16fp', '18fp').getValue(widthBp)
应用在折叠屏展开态页面初始化时的字体大小与md断点下的值不一致,导致折叠屏展开态打开应用和折叠态打开应用再展开,字体大小不一致。
修改代码中页面字体大小初始化逻辑,保证页面字体大小初始化时的值与对应断点下的值一致,这样就能保证折叠屏展开态打开应用和折叠态打开应用再展开,字体大小一致,代码样例如下。
- import { window } from '@kit.ArkUI';
- import { common } from '@kit.AbilityKit';
-
- export class WidthBreakpointType<T> {
- sm: T;
- md: T;
- lg: T;
-
- constructor(sm: T, md: T, lg: T) {
- this.sm = sm;
- this.md = md;
- this.lg = lg;
- }
-
- getValue(widthBp: WidthBreakpoint): T {
- if (widthBp === WidthBreakpoint.WIDTH_XS || widthBp === WidthBreakpoint.WIDTH_SM) {
- return this.sm;
- }
- if (widthBp === WidthBreakpoint.WIDTH_MD) {
- return this.md;
- } else {
- return this.lg;
- }
- }
- }
-
- @Entry
- @Component
- struct TextSizePage {
- private message: string = 'Hello World';
- // 页面字体大小默认值
- @State theFontSize: Resource | number | string = 16;
- @StorageProp('widthBp') widthBp: WidthBreakpoint | undefined = AppStorage.get('widthBp');
-
- getWindow(): window.Window {
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
- let windowStage = context.windowStage;
- let windowClass = windowStage.getMainWindowSync();
- return windowClass;
- }
-
- aboutToAppear(): void {
- // 按断点类型初始化页面字体大小
- if (this.widthBp) {
- this.theFontSize = new WidthBreakpointType('10fp', '16fp', '22fp').getValue(this.widthBp);
- }
- let windowClass: window.Window = this.getWindow();
- // 监听窗口尺寸的变化
- windowClass.on('windowSizeChange', () => {
- let uiContext: UIContext = windowClass.getUIContext();
- let widthBp: WidthBreakpoint = uiContext.getWindowWidthBreakpoint();
- // 改变应用全局状态变量中widthBp
- AppStorage.setOrCreate('widthBp', widthBp);
- // 根据窗口变化后的断点,改变页面字体大小
- if (this.widthBp) {
- this.theFontSize = new WidthBreakpointType('16fp', '22fp', '26fp').getValue(this.widthBp);
- }
- });
- }
-
- build() {
- RelativeContainer() {
- Text(this.message)
- .id('TextSizePageHelloWorld')
- .fontSize(this.theFontSize)
- .fontWeight(FontWeight.Bold)
- .alignRules({
- center: { anchor: '__container__', align: VerticalAlign.Center },
- middle: { anchor: '__container__', align: HorizontalAlign.Center }
- });
- }
- .height('100%')
- .width('100%');
- }
- }
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功