android APP字体大小,不随系统的字体大小变化而变化的方法
16lz
2021-01-26
从android4.0起系统设置的”显示“提供设置字体大小的选项。这个设置直接会影响到所有sp为单位的字体适配,所以很多app在设置了系统字体后瞬间变得面目全非。
Resourcesres=super.getResources();Configurationconfig=newConfiguration();config.setToDefaults();res.updateConfiguration(config,res.getDisplayMetrics());
虽然google推荐使用sp作为字体的单位,但实际的开发过程中通常是根据UIUE的设计稿来换算 sp(px换算sp)。而sp即使在同一种密度下其值也不尽相同。比如在240dpi的设备,如果是480x800分辨率这个值通常是1.5倍 (scaledDensity=1.5),如果是480xZ(z>800)那么这个值有可能大于1.5。这无疑给设备的适配带来更多的困难和陷阱。所以个人通常建议使用dpi来作为字体的单位
对于个别app不需要根据系统字体的大小来改变的,可以在activity基类(app中所有的activity都应该有继承于我们自己定义的一个BaseActivity类)中加上以下code:
@OverridepublicResourcesgetResources(){Resourcesres=super.getResources();Configurationconfig=newConfiguration();config.setToDefaults();res.updateConfiguration(config,res.getDisplayMetrics());returnres;}
更多相关文章
- Android(安卓)Permission 中文说明
- Android(安卓)Studio Checkout GitHub Error:cannot run program
- Android精通:TableLayout布局,GridLayout网格布局,FrameLayout帧布
- Paint和Color的介绍
- android定制化显示toast
- Android设置AlertDialog点击按钮对话框不关闭
- 通过自定义View,创建一个圆形指示器
- APP字体大小,不随系统的字体大小变化而变化的方法
- Android(安卓)ViewGroup.setDescendantFocusability函数