前言

由于Android开发中部分第三方应用字体过小,用户会调整Android系统的字体大小,但由于我们应用是定制化开发的,改变字体也会影响我们应用的字体显示。

因此需求:定制化的APP内字体大小不随系统设置变化。

正文

在Activity中重写如下方法

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    private Context getConfigurationContext(Context context){
        if(null== context){
            return  null;
        }
        Configuration configuration = context.getResources().getConfiguration();
        //重新配置字体放大的倍数。
        configuration.fontScale= 1;
        return context.createConfigurationContext(configuration);
    }

当然也有其他的方式,如下面的

public void setDefaultFontAndDisplay(Activity activity){
    Resources resources = activity.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.fontScale = 1.0f;//重新配置字体放大的倍数。
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

上面两个都可以,但我的一个项目中使用第二种,在AndroidManifest.xml配置了configChanges,如下

android:configChanges="screenSize|keyboard|keyboardHidden|layoutDirection|mcc|mnc|locale|touchscreen|orientation|locale|screenLayout|uiMode|fontScale|smallestScreenSize|navigation"

就存在问题。

事后发现,很可能是监听了[fontScale]导致的。

参考文章

  1. Android 字体大小(fontScale)不随系统设置变化_宋冠巡的博客-CSDN博客

相关文章

暂无评论

none
暂无评论...