慕后森
dp,dip,sp均为android中的概念。dp即dip(Density-independentpixel),抽象概念,在每英寸160像素(DPI=160)的屏幕上1dp=1px。Sowheredoyoubeginwhendesigningformultiplescreens?Oneapproachistoworkinthebasestandard(mediumsize,MDPI)andscaleitupordownfortheotherbuckets.Anotherapproachistostartwiththedevicewiththelargestscreensize,andthenscaledownandfigureouttheUIcompromisesyou'llneedtomakeonsmallerscreens.即一个android程序提供多套图片资源和配置文件就可以适应各种屏幕,比如一个程序定义一行四个按钮,每个按钮宽60dp,我的手机galaxynexus的4.65寸1280*720屏幕中(密度320DPI,1dp=2px),调用的每个图片都是宽120像素的,而在htc的3.8寸800*480的屏幕中(密度240DPI,1dp=1.5px)同样一行显示4个图片,每个图片宽90像素。由于长宽比例不同,实际效果会有长或宽的间隙不同的问题,估计可以在布局文件中调整,下面是官方文档中的定义。AvirtualpixelunitthatyoushouldusewhendefiningUIlayout,toexpresslayoutdimensionsorpositioninadensity-independentway.Thedensity-independentpixelisequivalenttoonephysicalpixelona160dpiscreen,whichisthebaselinedensityassumedbythesystemfora"medium"densityscreen.Atruntime,thesystemtransparentlyhandlesanyscalingofthedpunits,asnecessary,basedontheactualdensityofthescreeninuse.Theconversionofdpunitstoscreenpixelsissimple:px=dp*(dpi/160).Forexample,ona240dpiscreen,1dpequals1.5physicalpixels.Youshouldalwaysusedpunitswhendefiningyourapplication'sUI,toensureproperdisplayofyourUIonscreenswithdifferentdensities.