Android-app内部调整字体大小
app内部调整字体大小
·
将下面的方法复制在BaseActivity中,getTextSize()方法的数值可以根据实际情况自定义更多
//重写字体缩放比例 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 = getTextSize();
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 = getTextSize();
final Context newContext = newBase.createConfigurationContext(config);
super.attachBaseContext(newContext);
} else {
super.attachBaseContext(newBase);
}
}
//设置字体大、中、小
private float getTextSize() {
float fontScale;
int sizeType = AppSharePre.getInstance().getSizeType();
if (sizeType == 3) {
fontScale = (float) (1.3f);
} else if (sizeType == 2) {
fontScale = (float) (1.0f);
} else {
fontScale = (float) (0.8f);
}
return fontScale;
}
使用内部自定义字体大小时,在设置后重启MainActivity就可以了,将下内容复制到重启的方法中然后调用,记得将所有activity全部 finish() 了哟
Intent intent = getIntent(); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent);
更多推荐



所有评论(0)