Historically, programmers always designed UI in terms of pixels. For example, you might make a field 300 pixels wide, allow 5 pixels of spacing between columns, and define icons 16-by-16 pixels in size. The problem is that if you run that program on new
displays with more and more dots(pixels) per inch (dpi), the UI appears smaller and smaller.
Resolution-independent measurements help solve this problem. Android supports all the following units:
• px (pixels): Dots on the screen.
• in (inches): Size as measured by a ruler.
• pt (points): 1/72 of an inch.
• dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
• dip: Synonym for dp, used more often in Google examples.
• sp (scale-independent pixels): Similar to dp, but also scaled by the user’s font size preference.
To make your interface scalable to any current and future type of display, I recommend you always use the sp unit for text sizes and the dip unit for everything else. You should also consider using vector graphics instead of bitmaps
px:像素点
in:英寸
pt:磅,1/72 英寸
density:表示每英寸有多少个dots(pixels)
dp:一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1px
dip:等同于dp
sp:同dp相似,但还会根据用户选定的字体大小来缩放。
建议使用sp作为文本的单位,其它用dip。
dp与px的换算:(必须知道所用屏幕的density)
px = (density / 160) * dp
说白了,dp就是选定了160 pixel per inch的density为基准,可以根据实际的density自动scale到实际使用的pixel。
HVGA屏density=160;
QVGA屏density=120;
WVGA屏density=240;
WQVGA屏density=120
当屏幕density=240时,使用hdpi 标签的资源
当屏幕density=160时,使用mdpi标签的资源
当屏幕density=120时,使用ldpi标签的资源。
不加任何标签的资源是各种分辨率情况下共用的。
本文深入探讨了Android平台下UI设计中分辨率独立测量单位的重要性,包括px、in、pt、dp、dip、sp等单位的定义与应用,以及如何根据不同屏幕密度选择合适的单位以确保界面在不同设备上的适配性和一致性。

2304

被折叠的 条评论
为什么被折叠?



