dip: device independent pixels(设备独立像素). 一般我们为了支持多种分辨率如WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
dp: dip是一样的,谷歌官方推荐使用这个
px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较少。
pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。
每次遇到这个问题我都要好好想一遍,看了很多遍,有时还是会搞不清楚。强加记忆。
px is one pixel.
sp is scale-independent pixels.
dip is Density-independent pixels.
You would use
sp for font sizes
dip for everything else, where dip == dp
px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
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".
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.
dp: Density-independent Pixels
一个抽象的单元,基于屏幕的物理密度。
(dp和dip的意义相同,所以不用区别对待)。
这些单元是相对于160dpi(dots per inch)的屏幕说的,在160dpi的屏幕上,1dp粗略地等于1px。
当运行在更高密度的屏幕上的时候,要绘制1dp的像素数量会放大一个比例,这个比例就是和屏幕密度(dpi)相关。
类似的,在一个低密度的屏幕上,像素数目会缩小一个比例。
dp到px的这个比例将会随着屏幕的密度变化,而不是直接的比例关系。
用dp单位,而不是px,是一种简单的屏幕密度适配解决方式。
换句话说,它提供了一种方式,可以在多种设备上维持真实尺寸一致性。
sp:Scale-independent Pixels
这个有点像dp单位,但是它也根据用户的字体设置(font preference)缩放尺寸。
建议用这种尺寸单位来标注字体尺寸,这样它们将会因为屏幕密度和用户设定而调整。
pt
Points 1/72 inch(英寸),根据屏幕的物理尺寸。
px: Pixels
相应于真实的像素。
这种单位不被建议,因为真实的表达会根据设备的不同相差很远。
每个设备上每英寸的像素数不同(密度不同),并且屏幕上总的像素数也不同(整体大小不同)。