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

使用了AndroidAutoSize后,系统设置改变字体,切回App, Activity重建,但是布局文件内的字体不会自动适配改变 #447

Open
5 tasks done
moxin73 opened this issue Aug 2, 2023 · 4 comments

Comments

@moxin73
Copy link

moxin73 commented Aug 2, 2023

Environment

  • Autosize Version: 最新源码引入
  • Target Android Version: Android 12
  • Device Model: 三星 SM-N9860
  • Device Resolution: 1080px * 1920px
  • Design Size On AndroidManifest: 375dp * 812dp

Bug Description:

要求App内字体跟随系统设置字体大小改变,使用了AndroidAutoSize后,系统设置改变字体,切回App, Activity重建,但是布局文件内的字体不会自动适配改变。

手动杀死进程重启后字体就会改变了。查看了当前issues列表,没找到对应解决方案,请大神帮忙解惑

Log:

// 改变字体前日志:
The com.xxx.activity.MainActivity has been adapted!
MainActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 2.880000, targetScaledDensity = 5.760000, targetDensityDpi = 460, targetXdpi = 2.880000, targetScreenWidthDp = 375, targetScreenHeightDp = 713

 // 改变字体后日志:
MainActivity Info: isBaseOnWidth = true, designWidthInDp = 375.000000, designWidthInSubunits = 375.000000, targetDensity = 2.880000, targetScaledDensity = 5.760000, targetDensityDpi = 460, targetXdpi = 2.880000, targetScreenWidthDp = 375, targetScreenHeightDp = 713

Related Code:

  @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        AutoSize.clearCache();
        Activity activity = getTopActivity();
        if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            AutoSize.autoConvertDensityOfGlobal(activity);
        }
    }

Others:

@JessYanCoding
Copy link
Owner

看你这日志,影响字体的 targetScaledDensity 变量并没有变啊,
试试 Activity 重建前自己调用 setPrivateFontScale,是否有变化

@moxin73
Copy link
Author

moxin73 commented Aug 2, 2023

看你这日志,影响字体的 targetScaledDensity 变量并没有变啊, 试试 Activity 重建前自己调用 setPrivateFontScale,是否有变化

新增了:AutoSizeConfig.getInstance().setPrivateFontScale(getSystemFontScale()); 仍然无效

@JessYanCoding
Copy link
Owner

    @Override
    public Resources getResources() {
        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources());//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity(super.getResources(), 667, false);//如果有自定义需求就用这个方法
        return super.getResources();
    }

加上 getResources 试试。

@moxin73
Copy link
Author

moxin73 commented Aug 3, 2023

    @Override
    public Resources getResources() {
        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources());//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity(super.getResources(), 667, false);//如果有自定义需求就用这个方法
        return super.getResources();
    }

加上 getResources 试试。

感谢大神,好使了,最终解决方案:

Application中:

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (isFontScaleChanged()) {
            AutoSizeCompat.clearCache();
        }
    }

BaseActivity 中:

    @Override
    public Resources getResources() {
        AutoSizeConfig.getInstance().privateFontScale = getSystemFontScale()
         AutoSizeCompat.autoConvertDensityOfGlobal(resources)
        return super.getResources()
    }

这样就能自行改变适配了

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

2 participants