Android “px”、“dip”、“dp”和“sp”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2025282/
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
What is the difference between "px", "dip", "dp" and "sp"?
提问by capecrawler
What is the difference between Android units of measure?
Android 度量单位之间有什么区别?
- px
- dip
- dp
- sp
- 像素
- 蘸
- dp
- sp
采纳答案by Alex Volovoy
From the Android Developer Documentation:
px
Pixels- corresponds to actual pixels on the screen.in
Inches- based on the physical size of the screen.
1 Inch = 2.54 centimetersmm
Millimeters- based on the physical size of the screen.pt
Points- 1/72 of an inch based on the physical size of the screen.dpor dip
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".sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
px
像素- 对应于屏幕上的实际像素。在
寸-基于屏幕的物理尺寸。
1 英寸 = 2.54 厘米mm
毫米- 基于屏幕的物理尺寸。pt
点- 基于屏幕物理尺寸的 1/72 英寸。dp或dip
Density-independent Pixels - 基于屏幕物理密度的抽象单位。这些单位是相对于 160 dpi 屏幕而言的,因此 1 dp 是 160 dpi 屏幕上的一个像素。dp-to-pixel的比例会随着屏幕密度的变化而变化,但不一定成正比。注意:编译器同时接受“dip”和“dp”,尽管“dp”与“sp”更一致。sp
Scale-independent Pixels - 这就像 dp 单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户偏好进行调整。
From Understanding Density Independence In Android:
+----------------+----------------+---------------+-------------------------------+
| Density Bucket | Screen Density | Physical Size | Pixel Size |
+----------------+----------------+---------------+-------------------------------+
| ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
+----------------+----------------+---------------+-------------------------------+
| mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
+----------------+----------------+---------------+-------------------------------+
| hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
+----------------+----------------+---------------+-------------------------------+
| xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
+----------------+----------------+---------------+-------------------------------+
| xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
+----------------+----------------+---------------+-------------------------------+
| xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
+----------------+----------------+---------------+-------------------------------+
+---------+-------------+---------------+-------------+--------------------+
| Unit | Description | Units Per | Density | Same Physical Size |
| | | Physical Inch | Independent | On Every Screen |
+---------+-------------+---------------+-------------+--------------------+
| px | Pixels | Varies | No | No |
+---------+-------------+---------------+-------------+--------------------+
| in | Inches | 1 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| mm | Millimeters | 25.4 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| pt | Points | 72 | Yes | Yes |
+---------+-------------+---------------+-------------+--------------------+
| dp | Density | ~160 | Yes | No |
| | Independent | | | |
| | Pixels | | | |
+---------+-------------+---------------+-------------+--------------------+
| sp | Scale | ~160 | Yes | No |
| | Independent | | | |
| | Pixels | | | |
+---------+-------------+---------------+-------------+--------------------+
More info can be also be found in the Google Design Documentation.
更多信息也可以在Google 设计文档中找到。
回答by Bruiser
Pretty much everything about this and how to achieve the best support for multiple screens of different sizes and densities is very well documented here:
关于这方面的几乎所有内容以及如何实现对不同尺寸和密度的多个屏幕的最佳支持都在此处得到了很好的记录:
Screen size
Actual physical size, measured as the screen's diagonal. For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra-large.Screen density
The number of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen. For simplicity, Android groups all actual screen densities into six generalized densities: low, medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.Orientation
The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.Resolution
The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple:px = dp * (dpi / 160)
. For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
屏幕尺寸
实际物理尺寸,按屏幕对角线测量。为简单起见,Android 将所有实际屏幕尺寸分为四种通用尺寸:小、正常、大和超大。屏幕密度屏幕
物理区域内的像素数;通常称为 dpi(每英寸点数)。例如,与“正常”或“高”密度屏幕相比,“低”密度屏幕在给定物理区域内的像素较少。为简单起见,Android 将所有实际屏幕密度分为六种广义密度:低、中、高、超高、超高和超高。Orientation
从用户的角度来看屏幕的方向。这是横向或纵向,这意味着屏幕的纵横比分别是宽或高。请注意,不仅默认情况下不同的设备以不同的方向运行,而且当用户旋转设备时,方向可能会在运行时发生变化。分辨率
屏幕上物理像素的总数。添加对多屏幕的支持时,应用程序不能直接使用分辨率;应用程序应该只关心屏幕尺寸和密度,如广义尺寸和密度组所指定的。密度无关像素 (dp)
定义 UI 布局时应使用的虚拟像素单位,以与密度无关的方式表达布局尺寸或位置。密度无关像素相当于 160 dpi 屏幕上的一个物理像素,这是系统为“中等”密度屏幕假定的基线密度。在运行时,系统会根据正在使用的屏幕的实际密度,在必要时透明地处理 dp 单位的任何缩放。dp 单位到屏幕像素的转换很简单:px = dp * (dpi / 160)
. 例如,在 240 dpi 的屏幕上,1 dp 等于 1.5 个物理像素。在定义应用程序的 UI 时,应始终使用 dp 单位,以确保在不同密度的屏幕上正确显示 UI。
If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that, it is always a good thing to know the actual number of active devices that have a particular screen configuration.
如果您真的想为一种以上类型的设备开发 Android 应用程序,那么您应该至少阅读一次屏幕支持开发文档。除此之外,了解具有特定屏幕配置的活动设备的实际数量总是一件好事。
回答by devmiles.com
I will elaborate more on how exactly does dp convert to px:
我将详细说明 dp 是如何转换为 px 的:
- If running on an mdpi device, a
150 x 150 px
image will take up150 * 150 dp
of screen space. - If running on an hdpi device, a
150 x 150 px
image will take up100 * 100 dp
of screen space. - If running on an xhdpi device, a
150x150 px
image will take up75 * 75 dp
of screen space.
- 如果在 mdpi 设备上运行,
150 x 150 px
图像将占用150 * 150 dp
屏幕空间。 - 如果在 hdpi 设备上运行,
150 x 150 px
图像将占用100 * 100 dp
屏幕空间。 - 如果在 xhdpi 设备上运行,
150x150 px
图像将占用75 * 75 dp
屏幕空间。
The other way around: say, you want to add an image to your application and you need it to fill a 100 * 100 dp
control. You'll need to create different size images for supported screen sizes:
反过来说:假设您想向应用程序添加图像,并且需要它来填充100 * 100 dp
控件。您需要为支持的屏幕尺寸创建不同尺寸的图像:
100 * 100 px
image for mdpi150 * 150 px
image for hdpi200 * 200 px
image for xhdpi
100 * 100 px
mdpi 的图像150 * 150 px
hdpi 的图像200 * 200 px
xhdpi 的图像
回答by Amit Gajera
px - Pixels- point per scale corresponds to actual pixels on the screen.
px - 像素- 每个刻度的点对应于屏幕上的实际像素。
i - Inches- based on the physical size of the screen.
i - 英寸- 基于屏幕的物理尺寸。
mm - Millimeters- based on the physical size of the screen.
mm - 毫米- 基于屏幕的物理尺寸。
pt - Points- 1/72 of an inch based on the physical size of the screen.
pt - 点- 基于屏幕物理尺寸的 1/72 英寸。
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 - 与密度无关的像素- 基于屏幕物理密度的抽象单位。这些单位是相对于 160 dpi 屏幕而言的,因此一个单位是 160 dpi 屏幕上的dp
一个像素。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 recommended that 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
单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户偏好进行调整。
Take the example of two screens that are the same size but one has a screen density of 160 dpi (dots per inch, i.e. pixels per inch) and the other is 240 dpi.
以两个尺寸相同但屏幕密度为160 dpi(每英寸点数,即像素每英寸),另一个为240 dpi的屏幕为例。
Lower resolution screen Higher resolution, same size
Physical Width 1.5 inches 1.5 inches
Dots Per Inch (“dpi”) 160 240
Pixels (=width*dpi) 240 360
Density (factor of baseline 160) 1.0 1.5
Density-independent pixels 240 240
(“dip” or “dp” or “dps”)
Scale-independent pixels
(“sip” or “sp”) Depends on user font size settings same
回答by Sazzad Hissain Khan
Moreover you should have clear understanding about the following concepts:
此外,您应该清楚了解以下概念:
Screen size:
屏幕尺寸:
Actual physical size, measured as the screen's diagonal. For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra large.
实际物理尺寸,以屏幕的对角线测量。为简单起见,Android 将所有实际屏幕尺寸分为四种通用尺寸:小、正常、大和超大。
Screen density:
屏幕密度:
The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen. For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.
屏幕物理区域内的像素数量;通常称为 dpi(每英寸点数)。例如,与“正常”或“高”密度屏幕相比,“低”密度屏幕在给定物理区域内的像素较少。为简单起见,Android 将所有实际屏幕密度分为四种广义密度:低、中、高和超高。
Orientation:
方向:
The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.
从用户的角度来看屏幕的方向。这是横向或纵向,这意味着屏幕的纵横比分别是宽或高。请注意,不仅默认情况下不同的设备以不同的方向运行,而且当用户旋转设备时,方向可能会在运行时发生变化。
Resolution:
解析度:
The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.
屏幕上的物理像素总数。添加对多屏幕的支持时,应用程序不能直接使用分辨率;应用程序应该只关心屏幕尺寸和密度,如广义尺寸和密度组所指定的。
Density-independent pixel (dp):
与密度无关的像素 (dp):
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
定义 UI 布局时应使用的虚拟像素单元,以与密度无关的方式表达布局尺寸或位置。密度无关像素相当于 160 dpi 屏幕上的一个物理像素,这是系统为“中等”密度屏幕假定的基线密度。在运行时,系统会根据正在使用的屏幕的实际密度,在必要时透明地处理 dp 单位的任何缩放。dp 单位到屏幕像素的转换很简单:px = dp * (dpi / 160)。例如,在 240 dpi 的屏幕上,1 dp 等于 1.5 个物理像素。在定义应用程序的 UI 时,应始终使用 dp 单位,以确保在不同密度的屏幕上正确显示 UI。
Reference: Android developers site
回答by Mina Gabriel
dp
is dip
. Use it for everything (margin, padding, etc.).
dp
是 dip
。将它用于所有内容(边距、填充等)。
Use sp
for {text-size} only.
使用sp
了{文字大小}只。
To get the same size on different screen densities, Android translates these units into pixels at runtime, so there is no tricky math for you to do.
为了在不同的屏幕密度上获得相同的尺寸,Android 会在运行时将这些单位转换为像素,因此您无需进行复杂的数学运算。
See the difference between px
, dp
and sp
on different screen sizes.
查看px
,dp
和sp
在不同屏幕尺寸上的区别。
回答by chaitanya
回答by rds
Definitions
定义
pxor dot is a pixelon the physical screen.
px或 dot 是物理屏幕上的一个像素。
dpiare pixels per inch on the physical screen and represent the density of the display.
dpi是物理屏幕上每英寸的像素数,代表显示器的密度。
Android gives alias names to several densities
- ldpi (low) ~120dpi
- mdpi (medium) ~160dpi
- hdpi (high) ~240dpi
- most devices in 2015 are here
- xhdpi (extra-high) ~320dpi
- Apple iPhone 4/5/6, Nexus 4
- xxhdpi (extra-extra-high) ~480dpi
- Nexus 5
- xxxhdpi (extra-extra-extra-high) ~640dpi
- ldpi(低)~120dpi
- mdpi(中)~160dpi
- hdpi(高)~240dpi
- 2015 年的大多数设备都在这里
- xhdpi(超高)~320dpi
- 苹果 iPhone 4/5/6、Nexus 4
- xxhdpi(超高)~480dpi
- 连结 5
- xxxhdpi(超超超高)~640dpi
dipor dpare density-indenpendant pixels, i.e. they correspond to more or less pixels depending on the physical density.
dip或dp是与密度无关的像素,即它们对应于更多或更少的像素,具体取决于物理密度。
- 1dp = 1px on mdpi
- 1dp = 1px 在 mdpi
spor sipis a scale-independant pixel. They are scaled when the Large Textoption is turned on in Settings> Accessibility
sp或sip是与比例无关的像素。当在> 中打开大文本选项时,它们会被缩放SettingsAccessibility
- 1sp = 1dp
- 1sp = 1.2dp with accessibility Large Text
- 1sp = 1dp
- 1sp = 1.2dp 带有可访问性大文本
What to use?
用什么?
Use spfor Text size.
Use dpfor everything else.
使用sp作为文本大小。
将dp用于其他一切。
回答by sms247
Source 3: (data from source 3 is given below)
These are dimension values defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:
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
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommended that you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
px
Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
mm
Millimeters - Based on the physical size of the screen.
in
Inches - Based on the physical size of the screen.
这些是在 XML 中定义的维度值。维度由数字后跟度量单位指定。例如:10px、2in、5sp。Android 支持以下度量单位:
dp
密度无关像素 - 基于屏幕物理密度的抽象单位。这些单位相对于 160 dpi(每英寸点数)屏幕,在该屏幕上 1dp 大致等于 1px。在更高密度的屏幕上运行时,用于绘制 1dp 的像素数会按适合屏幕 dpi 的系数放大。同样,当在较低密度的屏幕上时,用于 1dp 的像素数会按比例缩小。dp-to-pixel的比例会随着屏幕密度的变化而变化,但不一定成正比。使用 dp 单位(而不是 px 单位)是使布局中的视图尺寸针对不同的屏幕密度正确调整大小的简单解决方案。换句话说,它为您的 UI 元素在不同设备上的真实尺寸提供了一致性。
sp
Scale-independent Pixels - 这就像 dp 单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户的喜好进行调整。
点
点数 - 基于屏幕物理尺寸的 1/72 英寸。
像素
像素 - 对应于屏幕上的实际像素。不建议使用此度量单位,因为实际表示可能因设备而异;每个设备每英寸可能有不同数量的像素,并且屏幕上可用的总像素可能更多或更少。
毫米
毫米 - 基于屏幕的物理尺寸。
在
英寸 - 基于屏幕的物理尺寸。
Note:A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one element.
注意:维度是使用名称属性中提供的值(而不是 XML 文件的名称)引用的简单资源。因此,您可以在一个 XML 文件中的一个元素下将维度资源与其他简单资源组合在一起。
回答by Joe Plante
Basically the only time where px applies is one px, and that's if you want exactly one pixel on the screen like in the case of a divider:
基本上 px 应用的唯一时间是 1 px,如果你想要屏幕上正好有一个像素,就像在分隔线的情况下一样:
On >160 dpi, you may get 2-3 pixels,
在 >160 dpi 上,您可能会得到 2-3 个像素,
On >120 dpi, it rounds to 0.
在 >120 dpi 时,它舍入为 0。