Android 手机提供了设置字体大小的功能,比如:
当然,app也可以实现统一管理字体大小(其实就是按字体大小的倍数设置),实现很简单
核心代码:
//改变字体大小的关键是改变getResources().getConfiguration().fontScale的值
public class BaseActivity extends AppCompatActivity {
//重写字体缩放比例 api<25
@Override
public Resources getResources() {
Resources res =super.getResources();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
Configuration config = res.getConfiguration();
config.fontScale = MyApplication.getFontSize();//设置正常字体大小的倍数
res.updateConfiguration(config,res.getDisplayMetrics());
}
return res;
}
//重写字体缩放比例 api>25
@Override
protected void attachBaseContext(Context newBase) {
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.N){
final Resources res = newBase.getResources();
final Configuration config = res.getConfiguration();
config.fontScale = MyApplication.getFontSize();//设置正常字体大小的倍数
final Context newContext = newBase.createConfigurationContext(config);
super.attachBaseContext(newContext);
}else{
super.attachBaseContext(newBase);
}
}
}
demo效果图: