强制字体不随着系统改变而改变
程序员文章站
2022-07-13 16:01:31
...
在Application中重写onConfigurationChanged 强制字体不随着系统改变而改变(微信也是这么干的)
public class App extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.fontScale != 1) {
getResources();
}
super.onConfigurationChanged(newConfig);
}
@Override
public Resources getResources() {
Resources res = super.getResources();
if (res.getConfiguration().fontScale != 1) {
//非默认值
Configuration newConfig = new Configuration();
newConfig.setToDefaults(); //设置默认
res.updateConfiguration(newConfig, res.getDisplayMetrics());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
createConfigurationContext(newConfig);
} else {
res.updateConfiguration(newConfig, res.getDisplayMetrics());
}
}
return res;
}
}