一钝域、 APP字體大小亭饵,不隨系統(tǒng)的字體大小變化而變化的方法
1咐扭、將字體大小的單位設(shè)置了dp华弓,就可以固定字體大小不隨系統(tǒng)設(shè)定的字號變化
sp和dp很類似但唯一的區(qū)別是食零,Android系統(tǒng)允許用戶自定義文字尺寸大小(小寂屏、正常贰谣、大、超大等等)迁霎,當(dāng)文字尺寸是“正持ǜВ”時1sp=1dp=0.00625英寸,而當(dāng)文字尺寸是“大”或“超大”時考廉,1sp>1dp=0.00625英寸频伤。
2、代碼設(shè)置(新)
● 新建類MyContextWrapper
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) { super(base);
}
@NonNull public static ContextWrapper wrap(Context context) {
Resources resources = context.getResources();
Configuration newConfig = new Configuration();
DisplayMetrics metrics = resources.getDisplayMetrics();
newConfig.setToDefaults(); //如果沒有設(shè)置densityDpi, createConfigurationContext對字體大小設(shè)置限制無效 newConfig.densityDpi = metrics.densityDpi;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(newConfig);
} else {
resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
}
return new MyContextWrapper(context);
}
}
● 在所有Activity(BaseActivity)添加
@Override protected void attachBaseContext(Context newBase) {
? ? super.attachBaseContext(MyContextWrapper.wrap(newBase));
}
updateConfiguration 設(shè)置會對其他Activity也有效芝此。
createConfigurationContext 自對當(dāng)前Activity有效憋肖。
3、代碼設(shè)置(過時)
1)在Application中加入
private void setTextDefault() {
private void setTextDefault() {?
?Resourcesres= super.getResources();
Configurationconfig= new Configuration();config.setToDefaults();
res.updateConfiguration(config, res.getDisplayMetrics());
}
缺點:如果home出來婚苹,更改了字體大小岸更,字體還是會改變。完全退出應(yīng)用在進去膊升,字體才會改為默認大小怎炊。
2)在所有Activity 中加入,改變字體大小能及時還原默認大小。
@Override public Resources getResources() {??
Resourcesresources= super.getResources();
if (resources.getConfiguration().fontScale != 1) {?
?? ConfigurationnewConfig= new Configuration();
newConfig.setToDefaults();
resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
}??
return resources;
}
二评肆、WebView 顯示html 字體大小不隨系統(tǒng)變化
webSettings.setTextZoom(100);//防止系統(tǒng)字體大小影響布局