Android 应该使用“sp”而不是“dp”来表示文本大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23981260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should use "sp" instead of "dp" for text sizes
提问by Peter Panne
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."
在我的 XML for a 中textView
,我收到一条警告“应该使用"sp"
而不是"dp"
用于文本大小。”
Why should "dp" not be used? What is the correct approach? How can I achieve same textsizes on different displays?
为什么不应该使用“dp”?正确的做法是什么?如何在不同的显示器上实现相同的文本大小?
回答by Ajit Singh
You should always use SP for fonts as it respects the user preferences. Here is an exampleLets understand it with the help of an example -
您应该始终将 SP 用于字体,因为它尊重用户的偏好。这是一个例子让我们借助一个例子来理解它 -
Text with SP and DP
带有 SP 和 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 不同。
回答by resource8218
You can use sp and dp. As you know in Android settings you can change text size (Settings -> My device -> Display -> Font size). All your textView in sp would change after changing font size in settings, dp - would not change
您可以使用 sp 和 dp。如您所知,您可以在 Android 设置中更改文本大小(设置 -> 我的设备 -> 显示 -> 字体大小)。在更改设置中的字体大小后,sp 中的所有 textView 都会更改,dp - 不会更改
回答by Giru Bhai
You may use both
你可以同时使用
spfor font sizes
dpfor everything else.
sp表示字体大小
dp表示其他所有内容。
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 screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
dp
Density-independent Pixels - 基于屏幕物理密度的抽象单位。这些单位是相对于 160 dpi 屏幕而言的,因此 1 dp 是 160 dpi 屏幕上的一个像素。dp-to-pixel的比例会随着屏幕密度的变化而变化,但不一定成正比。注意:编译器同时接受“dip”和“dp”,尽管“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 user's preference.
sp
Scale-independent Pixels - 这就像 dp 单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户偏好进行调整。
For more info see at Difference between px, dp, dip and sp in Android?
有关更多信息,请参阅Android 中 px、dp、dip 和 sp 之间的区别?
回答by Recomer
As @GiruBhai shared,it is more convenient to use sp instead of dp for the text size since it can be changed -unlike dp - according to the user's preferences.Which may be fulfilling your users needs better.
正如@GiruBhai 所分享的那样,使用 sp 而不是 dp 作为文本大小更方便,因为它可以根据用户的偏好进行更改 - 与 dp 不同。这可能会更好地满足您的用户需求。
More info. : Dimensions in Android
更多信息。: Android 中的尺寸
Source : developer.android.com
来源:developer.android.com