`
kerlubasola
  • 浏览: 678626 次
文章分类
社区版块
存档分类
最新评论

Dimension

 
阅读更多

A dimension value 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:

Android系统中,度量单位dimension是在XML文件中定义的。一个度量一般由一个具体的数值和对应的单位指定(以下介绍的dp、sp、pt等)。

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, so160dpis always one inchregardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens. (The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".)

在android系统中单位DP也就是DIP:device independent pixels(设备独立像素)。

dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

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 the user's preference.

缩放独立像素,主要用于字体。能根据用户设定的字体大小数值和屏幕的密度来调整显示大小。

pt

Points - 1/72 of an inch based on the physical size of the screen.

是一个标准的长度单位,1pt=1/72英寸。

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.



总结:dp也就是dip。这个和sp基本类似。如果设置表示长度、高度等属性时可以使用dp或sp。但如果设置字体,需要使用sp。dp是与密度无关,sp除了与密度无关外,还与scale无关。如果屏幕密度为160,这时dp和sp和px是一样的。1dp=1sp=1px,但如果使用px作单位,如果屏幕大小不变(假设还是3.2寸),而屏幕密度变成了320。那么原来TextView的宽度设成160px,在密度为320的3.2寸屏幕里看要比在密度为160的3.2寸屏幕上看短了一半。但如果设置成160dp或160sp的话。系统会自动将width属性值设置成320px的。也就是160 * 320 / 160。其中320 / 160可称为密度比例因子。也就是说,如果使用dp和sp,系统会根据屏幕密度的变化自动进行转换.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics