欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android中的尺寸单位 博客分类: android androidui 

程序员文章站 2024-03-06 19:46:14
...
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:

dp
dp 是与密度无关的像素,基于屏幕密度的抽象单位。在每英寸160个点的屏幕上,1dp与1px大致相等。当运行在更高/更低密度的屏幕上,1dp中的像素数根据屏幕的dpi(dots per inch)数而按比例决定。这个dp与(pixel)像素比例根据屏幕密度而变化。使用dp单位(而不是px单位) 是一个简单的解决方法,使我们的布局能自适应不同的屏幕密度,在不同的设备间能保持一致的展示真实的UI元素的尺寸。
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
sp 是与刻度无关的像素,与dp类似,建议使用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
pt,1/72 英寸。
Points - 1/72 of an inch based on the physical size of the screen.

px
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.
相关标签: android ui