对于文本大小,应该使用“sp”而不是“dp”吗

时间:2022-11-10 10:56:22

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."

在我的textView的XML中,我得到一个警告,“对于文本大小,应该使用sp而不是dp”。

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

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

4 个解决方案

#1


23  

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设置中,您可以更改文本大小(设置-> My device -> Display ->字体大小)。在设置中改变字体大小后,sp中的所有textView都将改变,而dp不会改变。

#2


23  

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 -

您应该始终对字体使用SP,因为它尊重用户的首选项。这里有一个例子让我们通过一个例子来理解它

Text with SP and DP

文本与SP和DP

对于文本大小,应该使用“sp”而不是“dp”吗

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

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

对于文本大小,应该使用“sp”而不是“dp”吗

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

现在重新打开app重新查看文本,你会看到使用SP的文本与DP的高度不同。

对于文本大小,应该使用“sp”而不是“dp”吗

#3


10  

You may use both

你可以同时使用

sp for font sizes
dp for 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".

一个基于屏幕物理密度的抽象单元。这些单位相对于160 dpi屏幕,所以一个dp是160 dpi屏幕上的一个像素。dpl与像素的比值将随屏幕密度的变化而变化,但不一定成正比。注意:编译器同时接受“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尺度无关的像素——这类似于dp单元,但它也可以根据用户的字体大小偏好进行缩放。建议您在指定字体大小时使用此单元,因此它们将根据屏幕密度和用户喜好进行调整。

For more info see at Difference between px, dp, dip and sp in Android?

要了解更多信息,请查看Android中px、dp、dip和sp的区别。

#4


0  

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

#1


23  

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设置中,您可以更改文本大小(设置-> My device -> Display ->字体大小)。在设置中改变字体大小后,sp中的所有textView都将改变,而dp不会改变。

#2


23  

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 -

您应该始终对字体使用SP,因为它尊重用户的首选项。这里有一个例子让我们通过一个例子来理解它

Text with SP and DP

文本与SP和DP

对于文本大小,应该使用“sp”而不是“dp”吗

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

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

对于文本大小,应该使用“sp”而不是“dp”吗

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

现在重新打开app重新查看文本,你会看到使用SP的文本与DP的高度不同。

对于文本大小,应该使用“sp”而不是“dp”吗

#3


10  

You may use both

你可以同时使用

sp for font sizes
dp for 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".

一个基于屏幕物理密度的抽象单元。这些单位相对于160 dpi屏幕,所以一个dp是160 dpi屏幕上的一个像素。dpl与像素的比值将随屏幕密度的变化而变化,但不一定成正比。注意:编译器同时接受“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尺度无关的像素——这类似于dp单元,但它也可以根据用户的字体大小偏好进行缩放。建议您在指定字体大小时使用此单元,因此它们将根据屏幕密度和用户喜好进行调整。

For more info see at Difference between px, dp, dip and sp in Android?

要了解更多信息,请查看Android中px、dp、dip和sp的区别。

#4


0  

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