应该使用“sp"而不是“dp"对于文字大小 [英] Should use "sp" instead of "dp" for text sizes

查看:17
本文介绍了应该使用“sp"而不是“dp"对于文字大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用

android:textSizes="20dp"

在我的 textView 的 XML 中,我收到警告应该使用 "sp" 而不是 "dp" 作为文本大小."

为什么不应该使用dp"?正确的做法是什么?如何在不同的显示器上实现相同的文本大小?

解决方案

您应该始终对字体使用 SP,因为它尊重用户的偏好.

更改设备文本设置(设置 -> 显示 -> 字体大小)

现在重新打开应用程序并重新查看文本,您会看到使用SP的文本与DP的高度不同.

When I use

android:textSizes="20dp" 

in my XML for a textView, I got a warning "Should use "sp" instead of "dp" for text sizes."

Why should "dp" not be used? What is the correct approach? How can I achieve same textsizes on different displays?

解决方案

You should always use SP for fonts as it respects the user preferences. Here is an example Lets understand it with the help of an example -

Text with SP and DP

Change the device text setting (Settings -> Display -> Font Size)

Now reopen the app and relook at the texts, You will see that the text which was using SP has different height than DP.

这篇关于应该使用“sp"而不是“dp"对于文字大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆