广告也精彩
目录

前言

由于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博客

 历史上的今天

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

相关文章

广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...

网站升级中

公告

近期网站升级中,可能存在一些bug。欢迎反馈 https://www.91es.com/we.html

本站域名

本站域名 : 91es.com3xcn.com。本站邮箱 : 站长邮箱 i@oorr.cn,通知邮箱we@oorr.cn ,如有更新,请看公告 。