android大字体模式,Android - 关于系统超大字体导致布局Bug

如题,最近遇到一些客户反馈回来的问题。截了个图过来,我一看字体完全撑大了我的LinearLayout 百思不得其解,作为一个专业的码农,我发誓这个代码肯定不是我写了,休想让我背锅。原因是用户设置了系统的字体所以导致偏好导致字体撑大,so 今天来解决一下这个问题。

谷歌官方教材建议的是采用sp做文字的单位。但是在实际开发的时候回发现一个问题,比如在标题布局的时候一般都会使用40dp或者50dp和32sp左右的字体。这样的话,在正常手机设置上会显得一个标题大小,这个没什么毛病。但是在一个设置了非默认字体的手机(超大号什么特大号的)这样就会导致布局受字体变大二变形。下面看一下官方对dp和sp的说明。

sp:

Scale-independent Pixels – This is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user’s preference.

dp:

Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.

sp 中文解说:

尺度独立的像素类似于dp单位,但是受用户偏好设置(就是系统字体大小)。推荐在使用字体设置的时候使用该单位,因此sp将根据屏幕密度和用户偏好设置。

dp 中文解说:

由于dp的太长了 我也不多说 ,相信大家都懂dp是什么鬼,这个dp不是重点。

总的来说就是 sp 会受到偏好设置影响,而dp则根据屏幕密度定。

下面来看两张图

57f9a52eadf7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

系统默认字体.jpg

57f9a52eadf7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

系统超大字体.png

当我的标题栏(白色地方layout_height为40dp的时候).理论上我设置32sp的字体大小就刚好完美显示我的标题.但是由于个别用户调皮 ,系统字体设置超大号,so 图二 ,我们代码使用sp的时候字体就会超出所固定的布局。

相信看完这篇文章的童鞋在往后的开发进行适配的时候一定会选用dp作为字体的单位。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值