Android sp vs dp texts - 什么会调整“比例”以及支持的理念是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11638691/
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
Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support
提问by Sam
So we know from many other posts that we should use sp rather than dp for text in Android, and we know the reason for this is to respect a 'user's preferences'.
所以我们从许多其他帖子中知道我们应该在 Android 中使用 sp 而不是 dp 来表示文本,我们知道这样做的原因是为了尊重“用户的偏好”。
But precisely what are these preferences? How might a user change this setting?
但这些偏好究竟是什么?用户如何更改此设置?
I cannot find any reference through the settings on my phone (I would have expected something in 'Accessibility' or 'Display'). So what is a user setting? Is it only done through the likes of an app such as 'Big Font'?
我无法通过手机上的设置找到任何参考(我本来希望在“辅助功能”或“显示”中有一些内容)。那么什么是用户设置?是否只能通过诸如“大字体”之类的应用程序来完成?
Assuming that it is (set by something like big font) - I have played with Google Docs and some other Google apps with the font set to 130%. While most layout stays fine, some gets a bit cut off and can't be read (and that is on a big screened SGS2). So, what is the approach to developing apps with text sizes using 'sp'? Do we make sure it works on 100% scaling and then ignore other settings - call it a special case that the user can worry about, or do we go out of our way to make sure things expand or are scrollable, in case the text overflows?
假设它是(由类似大字体的东西设置) - 我玩过谷歌文档和其他一些谷歌应用程序,字体设置为 130%。虽然大多数布局保持良好,但有些布局会被切断并且无法读取(这是在大屏幕 SGS2 上)。那么,使用“sp”开发具有文本大小的应用程序的方法是什么?我们是否确保它可以 100% 缩放,然后忽略其他设置 - 将其称为用户可以担心的特殊情况,或者我们是否会竭尽全力确保内容扩展或可滚动,以防文本溢出?
One argument is that we should use 'dp' to guaranteea user has a chance of seeing the text (even if they have to use a magnifying glass)
一个论点是我们应该使用 'dp' 来保证用户有机会看到文本(即使他们必须使用放大镜)
Thoughts/comments?
想法/评论?
回答by Joseph Earl
It is exposed in the settings menu on some Android devices (manufacturer dependent). It may also be altered by some accessibility options (device-dependent).
它在某些 Android 设备(取决于制造商)的设置菜单中公开。某些辅助功能选项(取决于设备)也可能会更改它。
In general, you should always used scale-independent pixels, especially for a large body of text.
通常,您应该始终使用与比例无关的像素,尤其是对于大量文本。
However if your text has to fit into a bounding-box of known size then you should use density independent pixels in order to ensure that the text always fits properly and that all characters are visible regardless of the users' setting.
但是,如果您的文本必须适合已知大小的边界框,那么您应该使用与密度无关的像素,以确保文本始终适合并且无论用户设置如何,所有字符都可见。
In a nutshell: would increasing the text-size by around 5sp result in the text being unreadable or mangle your UI? If so use density-independent pixels. If not, use scale-independent pixels. However you should generally aim to use scale-independent pixels wherever possible, which means designing a UI that can accommodate different text sizes.
简而言之:将文本大小增加大约 5sp 会导致文本不可读或破坏您的用户界面吗?如果是这样,请使用与密度无关的像素。如果不是,请使用与比例无关的像素。然而,您通常应该尽可能地使用与比例无关的像素,这意味着设计一个可以容纳不同文本大小的 UI。
回答by wsanville
Using the sp
unit is recommended for text because in ICS and above (could be Honeycomb too, correct me if I'm wrong), there isa preference for a user's font size. So, if you're using Gingerbread or lower, you won't be able to find this setting.
使用sp
单位建议文本,因为在ICS以上(可能是太蜂窝,纠正我,如果我错了),还有就是对用户的字体大小的偏好。因此,如果您使用 Gingerbread 或更低版本,您将无法找到此设置。
The preference is under Settings, Display, Font Size. There's also an option under Settings, Accessibility, Large text, too.
首选项位于设置、显示、字体大小下。在设置、辅助功能、大文本下也有一个选项。
To address your question about how to go about using sp
, note that by default, without changing any of the font size preferences, 1sp
is equivalent to 1dp
(also, they are equivalent before the preference was introduced). Like you've noted, designing for the case where a user has huge text would probably require you to assume things are going to need to scroll where you might otherwise not expect them to.
要解决您关于如何使用 的问题sp
,请注意,默认情况下,在不更改任何字体大小首选项的情况下,1sp
等效于1dp
(此外,在引入首选项之前它们是等效的)。就像您已经注意到的那样,针对用户拥有大量文本的情况进行设计可能需要您假设事情将需要滚动到您可能不希望它们滚动的地方。
回答by dessertcook
The answer lies in looking at this particular issue holistically.
答案在于从整体上看待这个特定问题。
The motivation for using "sp" for font sizes lies in giving the developer power to control their layout in the face of user changing the font size on their device.
使用“sp”作为字体大小的动机在于赋予开发人员在用户更改其设备上的字体大小时控制其布局的权力。
Example:
例子:
Lets look at 2 extreme cases:
让我们看看2个极端情况:
1) User selects font size "small"
1) 用户选择字体大小“小”
This is what my layout looks like:
这是我的布局的样子:
http://postimg.org/image/kiyqeo2bh/
http://postimg.org/image/kiyqeo2bh/
Here is the layout xml:
这是布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:text="Material-Design ist die Schei?e"
android:textSize="18sp"
android:background="#ffff0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:text="Material-Design ist die Schei?e"
android:textSize="25sp"
android:background="#ffff0000" />
2) If the user selects font size "huge":
2)如果用户选择字体大小“巨大”:
This i what my layout looks like:
这就是我的布局:
http://postimg.org/image/d7rax9wob/
http://postimg.org/image/d7rax9wob/
My layout xml is same as above in case 1).
我的布局 xml 与上面的情况 1) 相同。
So, as you can see what happened here is the top TextView has sort of perfect font-size in sp because it does not wrap for the entirety of the range of font sizes (small to huge). But the bottom TextView completely messes up your layout/design in case 2).
因此,正如您所看到的,顶部 TextView 在 sp 中具有完美的字体大小,因为它不会对整个字体大小范围(从小到大)进行换行。但是底部的 TextView 在情况 2) 中完全弄乱了您的布局/设计。
So you as a developer can iterate and decide what size in sp works for your design and android will draw it for you.
因此,您作为开发人员可以迭代并决定 sp 中的大小适合您的设计,而 android 将为您绘制它。