一般在activity里面添加如下代码
@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());
}
return res;
}
另外,比如在代码层自定义的文本大小( textView.setTextSize(15f);
),用上面的方法或许无效。
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,15); //15像素
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15); //15SP
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);//15DIP ----代码层,用这个就不会出现,字体跟随系统字体大小了