在activity的基类里面重写以下方法:(自留用)
//系统设置发生改变的时候会触发以下方法:(如屏幕方向,键盘和文字大小等) @Override public void onConfigurationChanged(Configuration newConfig) { //不是默认值 if (newConfig.fontScale != 1) getResources(); super.onConfigurationChanged(newConfig); } //重写 getResource() 方法,修改 configuration 为 setToDefaults() @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; }
也可以在application的oncreate中设置:
// 加载系统默认设置,字体不随用户设置变化 Resources res = super.getResources(); Configuration config = new Configuration(); config.setToDefaults(); res.updateConfiguration(config, res.getDisplayMetrics());
也可以在application中重写getResources;
还有一个是网上提供的方法,将字体设置为dp,没有试过,有意向的可以尝试一下。
感谢下面的博主: