首页 > 开发 > Android > 正文

android各种单位都是什么意思

2017-09-08 15:24:52  来源:网友分享

各种单位 px, dip, dp, sp
在Android里分别是个什么意思?体现在屏幕上是怎么算的?

解决方案

dp,dip,sp均为android中的概念。

dp即dip(Density-independent pixel),抽象概念,在每英寸160像素(DPI=160)的屏幕上1dp=1px。

So where do you begin when designing for multiple screens? One approach is to work in the base standard (medium size, MDPI) and scale it up or down for the other buckets. Another approach is to start with the device with the largest screen size, and then scale down and figure out the UI compromises you'll need to make on smaller screens.

即一个android程序提供多套图片资源和配置文件就可以适应各种屏幕,比如一个程序定义一行四个按钮,每个按钮宽60dp, 我的手机galaxy nexus的4.65寸1280*720屏幕中(密度320DPI,1dp=2px),调用的每个图片都是宽120像素的,而在htc的3.8寸800*480的屏幕中(密度240DPI,1dp=1.5px)同样一行显示4个图片,每个图片宽90像素。由于长宽比例不同,实际效果会有长或宽的间隙不同的问题,估计可以在布局文件中调整,下面是官方文档中的定义。

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.