Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android14 设置不跟随系统字体大小改变失效 #459

Open
5 tasks done
xiaolunan opened this issue Dec 20, 2023 · 4 comments
Open
5 tasks done

Android14 设置不跟随系统字体大小改变失效 #459

xiaolunan opened this issue Dec 20, 2023 · 4 comments

Comments

@xiaolunan
Copy link

Environment

  • Autosize Version: v1.2.1
  • Target Android Version: api34 android14
  • Device Model: Pixel 6 Pro
  • Device Resolution: 1440x3120
  • Design Size On AndroidManifest: 375*812

Bug Description:

//是否屏蔽系统字体大小对 AndroidAutoSize 的影响, 如果为 true, App 内的字体的大小将不会跟随系统设置中字体大小的改变
//如果为 false, 则会跟随系统设置中字体大小的改变, 默认为 false
AutoSizeConfig.getInstance().setExcludeFontScale(true);
设置了以上代码在安卓14中更改系统字体,app内字体还是会跟着变大(改变),在安卓14以下设备则有效

Log:

2023-12-20 10:46:25.669 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  designWidthInDp = 375, designHeightInDp = 812, screenWidth = 1440, screenHeight = 2900
2023-12-20 10:46:25.669 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  initDensity = 3.5, initScaledDensity = 4.5499997
2023-12-20 10:46:26.203 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  com.yihubang.manager.activitys.StartActivity used the global configuration.
2023-12-20 10:46:26.206 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  The com.yihubang.manager.activitys.StartActivity has been adapted! 
                                                                                                    StartActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 3.840000, targetScaledDensity = 3.840000, targetDensityDpi = 614, targetXdpi = 3.840000, targetScreenWidthDp = 375, targetScreenHeightDp = 733
2023-12-20 10:46:26.276 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  com.yihubang.manager.activitys.StartActivity used the global configuration.
2023-12-20 10:46:26.278 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  The com.yihubang.manager.activitys.StartActivity has been adapted! 
                                                                                                    StartActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 3.840000, targetScaledDensity = 3.840000, targetDensityDpi = 614, targetXdpi = 3.840000, targetScreenWidthDp = 375, targetScreenHeightDp = 733
---------------------------- PROCESS STARTED (11686) for package com.yihubang.manager ----------------------------
2023-12-20 10:46:27.878 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  com.yihubang.manager.activitys.AdminMainActivity used the global configuration.
2023-12-20 10:46:27.880 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  The com.yihubang.manager.activitys.AdminMainActivity has been adapted! 
                                                                                                    AdminMainActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 3.840000, targetScaledDensity = 3.840000, targetDensityDpi = 614, targetXdpi = 3.840000, targetScreenWidthDp = 375, targetScreenHeightDp = 733
2023-12-20 10:46:28.372 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  com.yihubang.manager.activitys.AdminMainActivity used the global configuration.
2023-12-20 10:46:28.375 11621-11621 AndroidAutoSize         com.yihubang.manager                 D  The com.yihubang.manager.activitys.AdminMainActivity has been adapted! 
                                                                                                    AdminMainActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 3.840000, targetScaledDensity = 3.840000, targetDensityDpi = 614, targetXdpi = 3.840000, targetScreenWidthDp = 375, targetScreenHeightDp = 733

Screenshot

Screenshot_20231220_104942
Screenshot_20231220_105013
Screenshot_20231220_104918

Related Code:

//是否屏蔽系统字体大小对 AndroidAutoSize 的影响, 如果为 true, App 内的字体的大小将不会跟随系统设置中字体大小的改变
//如果为 false, 则会跟随系统设置中字体大小的改变, 默认为 false
AutoSizeConfig.getInstance().setExcludeFontScale(true);

Others:

在真机上,小米14(Android14)上面测试也是这个问题。
Android14还有一个问题就是 AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));导致软键盘无法弹出。
删除以下代码解决了
@OverRide
public Resources getResources() {
//需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));
return super.getResources();
}

@sy5251
Copy link

sy5251 commented Apr 8, 2024

找到解决办法了吗?现在setExcludeFontScale(true);无效

@jusyps
Copy link

jusyps commented Apr 26, 2024

应用内有设置语言,然后在attachBaseContext里面设置fontScale为1,可以解决

@cxDosx
Copy link

cxDosx commented Jul 7, 2024

在Activity内做如下调整可以解决

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        overrideFontScale(newBase)
    }

    private fun overrideFontScale(context: Context?) {
        context?:return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            context.resources.configuration.let {
                it.fontScale = 1f
                applyOverrideConfiguration(it)
            }
        }
    }

@TokenYc
Copy link

TokenYc commented Oct 18, 2024

本质原因是Android14开始,字体缩放是非线性的,导致DisplayMetrics.scaledDensity这个方法失效,AutoSize在初始化字体缩放时失败。
改成用configuration.fontScale来设置就可以了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants