Android-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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值