“px”,“dip”,“dp”和“sp”有什么区别?

以下是什么区别?

  • PX
  • DP
  • SP

从Android开发者文档 :

  1. PX
    像素 – 对应于屏幕上的实际像素。


  2. 英寸 – 基于屏幕的物理尺寸。
    1英寸= 2.54厘米

  3. 毫米
    毫米 – 基于屏幕的物理尺寸。

  4. PT
    点 – 基于屏幕物理尺寸的1/72英寸。

  5. dpdip
    与密度无关的像素 – 一个基于屏幕物理密度的抽象单位。 这些单位是相对于一个160 dpi的屏幕,所以一个DP是160 dpi屏幕上的一个像素。 dp与像素的比率将随着屏幕密度而改变,但不一定成正比。 注:编译器同时接受“dip”和“dp”,但“dp”与“sp”更加一致。

  6. SP
    与比例无关的像素 – 这就像dp单位一样,但是也会根据用户的字体大小偏好进行缩放。 build议您在指定字体大小时使用本机,以便根据屏幕密度和用户偏好进行调整。

在Android中了解密度独立 :

 +----------------+----------------+---------------+-------------------------------+ | Density Bucket | Screen Density | Physical Size | Pixel Size | +----------------+----------------+---------------+-------------------------------+ | ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px | +----------------+----------------+---------------+-------------------------------+ | mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px | +----------------+----------------+---------------+-------------------------------+ | hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px | +----------------+----------------+---------------+-------------------------------+ | xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px | +----------------+----------------+---------------+-------------------------------+ | xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px | +----------------+----------------+---------------+-------------------------------+ | xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px | +----------------+----------------+---------------+-------------------------------+ 
 +---------+-------------+---------------+-------------+--------------------+ | Unit | Description | Units Per | Density | Same Physical Size | | | | Physical Inch | Independent | On Every Screen | +---------+-------------+---------------+-------------+--------------------+ | px | Pixels | Varies | No | No | +---------+-------------+---------------+-------------+--------------------+ | in | Inches | 1 | Yes | Yes | +---------+-------------+---------------+-------------+--------------------+ | mm | Millimeters | 25.4 | Yes | Yes | +---------+-------------+---------------+-------------+--------------------+ | pt | Points | 72 | Yes | Yes | +---------+-------------+---------------+-------------+--------------------+ | dp | Density | ~160 | Yes | No | | | Independent | | | | | | Pixels | | | | +---------+-------------+---------------+-------------+--------------------+ | sp | Scale | ~160 | Yes | No | | | Independent | | | | | | Pixels | | | | +---------+-------------+---------------+-------------+--------------------+ 

更多信息可以在Googledevise文档中find 。

要计算实际设备上的尺寸,可以使用此应用程序。

几乎所有这一切,以及如何实现不同大小和密度的多个屏幕的最佳支持是非常详细的文件在这里:

  • 支持多个屏幕

屏幕尺寸
实际的物理尺寸,按屏幕的对angular线测量。 为了简单起见,Android将所有实际的屏幕尺寸分为四种广义尺寸:小尺寸,普通尺寸,大尺寸和超大尺寸。

屏幕密度
屏幕物理区域内的像素数量; 通常称为dpi(每英寸点数)。 例如,与“正常”或“高”密度屏幕相比,“低”密度屏幕在给定的物理区域内具有更less的像素。 为了简单起见,Android将所有实际的屏幕密度分为六个广义密度:低,中,高,超高,超高和超高。

方向
从用户的angular度来看屏幕的方向。 这是横向或纵向,这意味着屏幕的宽高比分别是宽或高。 请注意,不同设备默认情况下在不同的方向上运行,但是在用户旋转设备时,方向会在运行时更改。

parsing度
屏幕上物理像素的总数。 在添加对多个屏幕的支持时,应用程序不能直接使用parsing; 应用程序应只涉及屏幕大小和密度,如广义大小和密度组所指定。

密度独立像素(dp)
定义UI布局时应使用的虚拟像素单位,以密度无关的方式表示布局尺寸或位置。 与密度无关的像素相当于160dpi屏幕上的一个物理像素,这是系统为“中等”密度屏幕假定的基准密度。 在运行时,系统根据所使用的屏幕的实际密度,根据需要透明地处理dp单位的任何缩放比例。 dp单位转换为屏幕像素很简单: px = dp * (dpi / 160) 。 例如,在240 dpi屏幕上,1 dp等于1.5个物理像素。 定义应用程序的UI时,应始终使用dp单位,以确保在具有不同密度的屏幕上正确显示您的UI。

如果您对开发适用于多种types设备的Android应用程序有任何认真的看法,您应该至less阅读一次屏幕支持开发文档。 除此之外,了解具有特定屏幕configuration的活动设备的实际数量总是一件好事。

  • 屏幕尺寸和密度

我将详细说明如何将dp转换为px:

  • 如果在mdpi设备上运行,150×150像素图像将占用150 * 150dp的屏幕空间。
  • 如果在hdpi设备上运行,150×150像素的图像将占用100 * 100dp的屏幕空间。
  • 如果在xhdpi设备上运行,150×150像素图像将占用75 * 75dp的屏幕空间。

反过来说,你要添加一个图像到你的应用程序,你需要它来填充一个100 * 100的DP控制。 您需要为支持的屏幕尺寸创build不同尺寸的图片:

  • 100 * 100像素为mdpi的图像
  • hdpi的150 * 150像素图像
  • xhdpi的200 * 200像素图像

px像素 – 每个比例点对应于屏幕上的实际像素。

以英寸为单位 – 基于屏幕的物理尺寸。

mm毫米 – 基于屏幕的物理尺寸。

点数 – 基于屏幕物理尺寸的1/72英寸。

dp密度独立像素 – 基于屏幕物理密度的抽象单位。 这些单位是相对于一个160 dpi的屏幕,所以一个dp是160 dpi屏幕上的一个像素。 dp与像素的比率将随着屏幕密度而改变,但不一定成正比。 注意:编译器同时接受dipdp ,但dpsp更加一致。

sp -Scale独立像素 – 这就像dp单元一样,但是也可以通过用户的字体大小首选项来缩放。 build议您在指定字体大小时使用本机,因此将根据屏幕密度和用户偏好进行调整。

以两个大小相同但屏幕密度为160 dpi(每英寸点数,即每英寸像素)的屏幕为例,另一个为240 dpi。

  Lower resolution screen Higher resolution, same size Physical Width 1.5 inches 1.5 inches Dots Per Inch (“dpi”) 160 240 Pixels (=width*dpi) 240 360 Density (factor of baseline 160) 1.0 1.5 Density-independent Pixels 240 240 (“dip” or “dp” or “dps”) Scale-independent pixels (“sip” or “sp”) Depends on user font size settings same 

此外,您应该清楚了解以下概念:

屏幕尺寸:

实际的物理尺寸,按屏幕的对angular线测量。 为了简单起见,Android将所有实际的屏幕尺寸分为四种广义尺寸:小尺寸,普通尺寸,大尺寸和超大尺寸。

屏幕密度:

屏幕物理区域内的像素数量; 通常称为dpi(每英寸点数)。 例如,与“正常”或“高”密度屏幕相比,“低”密度屏幕在给定的物理区域内具有更less的像素。 为了简单起见,Android将所有实际的屏幕密度分为四个广义密度:低,中,高和超高。

方向:

从用户的angular度来看屏幕的方向。 这是横向或纵向,这意味着屏幕的宽高比分别是宽或高。 请注意,不同的设备默认情况下会以不同的方向运行,但是在用户旋转设备时,运行方向会发生变化。

parsing度:

屏幕上物理像素的总数。 在添加对多个屏幕的支持时,应用程序不能直接使用parsing; 应用程序应只涉及屏幕大小和密度,如广义大小和密度组所指定。

与浓度无关的像素(dp):

定义UI布局时应使用的虚拟像素单位,以密度无关的方式表示布局尺寸或位置。 与密度无关的像素相当于160dpi屏幕上的一个物理像素,这是系统为“中等”密度屏幕假定的基准密度。 在运行时,系统根据所使用的屏幕的实际密度,根据需要透明地处理dp单位的任何缩放比例。 dp单位转换为屏幕像素很简单:px = dp *(dpi / 160)。 例如,在240 dpi屏幕上,1 dp等于1.5个物理像素。 定义应用程序的UI时,应始终使用dp单位,以确保在具有不同密度的屏幕上正确显示您的UI。

参考: Android开发人员网站

dpdip 。 使用它的一切(边距,填充等)。

仅为{text-size}使用sp


为了在不同的屏幕密度上获得相同的尺寸,Android会在运行时将这些单位转换为像素,所以不会有棘手的math问题。


查看不同屏幕尺寸的pxdpsp之间的差异。

在这里输入图像说明

来源: Android编程:大书呆子牧场指南

我已经计算出下面的公式,将dpi转换为dpsp 在这里输入图像描述

来源1

来源2

来源3 🙁 来源3的数据如下)

这些是以XML定义的维度值。 一个维度是用一个数字后跟一个度量单位来指定的。 例如:10px,2in,5sp。 Android支持以下计量单位:

DP

与密度无关的像素 – 基于屏幕物理密度的抽象单位。 这些单位是相对于160 dpi(每英寸点数)的屏幕,其中1dp大约等于1px。 在较高密度的屏幕上运行时,用于绘制1dp的像素数量将按照适合屏幕的dpi的比例放大。 同样,在密度较低的屏幕上,用于1dp的像素数量将缩小。 dp与像素的比率将随着屏幕密度而改变,但不一定成正比。 使用dp单位(而不是px单位)是一个简单的解决scheme,使您的布局中的视图尺寸正确resize不同的屏幕密度。 换句话说,它为不同设备上的UI元素的实际尺寸提供一致性。

SP

与刻度无关的像素 – 这就像dp单位一样,但是也会根据用户的字体大小偏好进行缩放。 build议您在指定字体大小时使用本机,因此将根据屏幕密度和用户偏好进行调整。

PT

点 – 基于屏幕物理尺寸的1/72英寸。

PX

像素 – 对应于屏幕上的实际像素。 不build议使用这个单位,因为实际的表示可以在不同的设备上有所不同。 每个设备可以具有不同数量的每英寸像素,并且可以具有更多或更less的屏幕上可用的总像素。

毫米

毫米 – 基于屏幕的物理尺寸。

英寸 – 基于屏幕的物理尺寸。

注意:维是使用name属性中提供的值(而不是XML文件的名称)引用的简单资源。 因此,您可以将维度资源与一个XML文件中的其他简单资源合并到一个元素下。

定义

px或dot是物理屏幕上的一个像素

dpi是物理屏幕上每英寸的像素,代表显示器的密度。

Android给别名提供了不同的密度

  • ldpi(低)〜120dpi
  • mdpi(中)〜160dpi
  • hdpi(高)〜240dpi
    • 2015年的大部分设备都在这里
  • xhdpi(超高)〜320dpi
    • 苹果iPhone 4/5/6,Nexus 4
  • xxhdpi(超高)〜480dpi
    • Nexus 5
  • xxxhdpi(extra-extra-extra-high)〜640dpi

dipdp密度不相关的像素 ,即它们对应于更多或更less的像素,这取决于物理密度。

  • 1dp = 1px在mdpi上

在这里输入图像描述

spsip是一个与尺度无关的像素 。 在“设置” >“ 辅助function”中打开“ 大文本”选项时,会缩放它们

  • 1sp = 1dp
  • 1sp = 1.2dp,可访问大文本

使用什么?

使用sp作为文本大小。

使用DP的一切。

基本上唯一一个px适用的时间是一个像素,这就是如果你想在屏幕上只有一个像素的情况下,像一个分频器。 在> 160上,您可能会获得2-3个像素,在120 dpi上,它会舍入为0。

PX

像素 – 对应于屏幕上的实际像素。

dp或dip

与密度无关的像素 – 一个基于屏幕物理密度的抽象单位。 这些单位是相对于一个160 dpi的屏幕,所以一个DP是160 dpi屏幕上的一个像素。

使用dp:

密度独立性 – 当应用程序在用不同密度的屏幕上显示时,保留用户界面元素的物理尺寸(从用户的angular度来看),实现“密度独立性”。 (即)在不同types的屏幕上,图像看起来应该是相同的大小(不放大或缩小)。

SP

与比例无关的像素 – 这就像dp单位一样,但是也会根据用户的字体大小偏好进行缩放。

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

在哪里可以使用px&dp之间的关系?

密度独立像素(dp)

定义UI布局时应使用的虚拟像素单位,以密度无关的方式表示布局尺寸或位置。 如上所述,与密度无关的像素相当于160dpi屏幕上的一个物理像素,这是系统为“中等”密度屏幕假设的基准密度。 在运行时,系统根据所使用的屏幕的实际密度,根据需要透明地处理dp单位的任何缩放比例。 dp单位到屏幕像素的转换很简单:

px = dp *(dpi / 160)。

例如,在240 dpi屏幕上,1 dp等于1.5个物理像素。 定义应用程序的UI时,应始终使用dp单位,以确保在具有不同密度的屏幕上正确显示您的UI。

了解像素到DP,反之亦然是非常重要的(特别是给创意团队确切的DP值)

 dp = px * 160 / dpi MDPI = 160 dpi || Therefore, on MDPI 1 px = 1 dp For example, if you want to convert 20 pixel to dp, use the above formula, dp = 20 * 160 / 160 = 20. So, 20 pixel = 20 dp. HDPI = 240 dpi - So, on HDPI 1.5 px = 1 dp XHDPI = 320 dpi - So, on XHDPI 2 px = 1 dp XXHDPI = 480 dpi - So, on XXHDPI 3 px = 1 dp For example, let us consider Nexus 4. If 24 pixels to be converted to dp and if it is a Nexus 4 screen, developers can convert it to dp easily by the following calculation : dp = 24 * 160 / 320 = 12 dp Screen dimension: 768 x 1280 pixel resolution (320 ppi or 320dpi) Optional (screen size): 4.7" diagonal 
  • 尝试从广告素材小组中获取偶数的所有像素值。 否则精度损失会与0.5相乘。

PX

上面已经解释过了。 尽量避免在布局文件中。 但有些情况下,需要px。 例如,ListView分隔线。 px在这里更好,因为在所有的屏幕分辨率下,给出一个像素线作为分频器。

SP

使用字体大小的SP。 然后,只有设备字体大小发生变化(即显示 – >设备上的字体),应用程序内部的字体才会发生变化。 如果你想在应用程序中保留一个静态大小的字体,你可以在dp中给出字体维度。 在这种情况下,它永远不会改变。 开发人员可能会得到一些特定屏幕的要求,因此开发人员可以使用dp而不是sp。 在其他情况下,build议使用sp。

您可以从下图中看出pxdp之间的差异,您也可以发现pxdp不能保证不同屏幕上的物理尺寸相同。

在这里输入图像描述

任何与文字和外观的大小相关的东西都必须使用sppt 。 而任何与控件的大小,布局等相关的东西都必须和dp一起使用。

你可以在地方使用dpdip

我只会使用dp。

有很多关于使用“sp”来表示字体大小的讨论,虽然我很欣赏这一点,但我认为从devise的angular度来看这不是正确的做法。 如果用户select了一些不可思议的字体大小,你最终可能会破坏你的devise,用户最终会责怪应用程序 ,而不是自己的生活select。

另外,如果您在160 dpi的平板电脑上使用sp字体应用程序,则会发现所有内容都可以扩展……但是相比之下,您的字体却显得微乎其微。 这不是一个好看。

虽然“sp”字体的想法有一颗好心,但这是一个不好的主意。 坚持与一切的DP。

通过从官方文档复制的答案提到的“ 用户字体大小首选项 ”中提到的dpsp单位之间的区别可以在运行时通过更改Settings->Accessibility->Large Text选项来Settings->Accessibility->Large Text

Large Text选项强制文本变大1.3倍。

 private static final float LARGE_FONT_SCALE = 1.3f; 

这可能当然是供应商的依赖,因为它位于软件包/应用程序/设置 。

sp =比例独立像素

dp = dip =密度独立像素

dpi =每英寸点数

我们应该避免使用sp

我们应该使用dp来支持多个屏幕。

Android支持不同的屏幕分辨率

  • ldpi(低)〜120 dpi mdpi(中)〜160 dpi hdpi(高)〜240 dpi xhdpi
  • (超高)〜320 dpi xxhdpi(超高)〜480 dpi xxxhdpi
  • (额外超高)〜640 dpi

120dp ldpi设备具有1英寸大小的120个像素。

其他密度也一样

我们作为软件工程师应该使用这个转换公式:

像素= dp *(密度/ 160)

所以240dpi设备的1dp将具有= 1 *(240/160)= 3/2 = 1.5像素。

而480dpi设备的1dp将有= 1 *(480/160)= 3像素。

使用这个1.5和3像素的知识,软件工程师可以devise不同密度的布局。

要检查任何设备的屏幕参数:

 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); Toast.makeText( this, "4:" + metrics.heightPixels + "," + metrics.density + "," + metrics.densityDpi, Toast.LENGTH_LONG).show(); 

dpi –

  • 每英寸的点数
  • 测量屏幕的像素密度。

像素 – 像素

  • 用于映射屏幕像素

点 – 点

  • 约1/72英寸,相对于物理屏幕尺寸。

英寸 – 相对于物理屏幕尺寸(1英寸= 2.54厘米)。

毫米 – 相对于物理屏幕尺寸。

与尺度无关的像素。

  • 基于用户的字体大小偏好。
  • 字体应该在'sp'中。

浸 –

  • dip == dp
  • 密度独立像素。
  • 它根据屏幕密度而变化。
  • 在160 dpi的屏幕上,1 dp = 1像素。
  • 使用除文本字体大小以外的dp。

在标准中,使用了dp和sp。 sp代表字体大小,dp代表其他所有内容。

单位换算公式:

px = dp *(dpi / 160);

 Density Bucket -> Screen Display => Physical Size => Pixel Size ldpi -> 120 dpi => 0.5 x 0.5 in => 0.5 in * 120 dpi = 60x60 px mdpi -> 160 dpi => 0.5 x 0.5 in => 0.5 in * 160 dpi = 80x80 px hdpi -> 240 dpi => 0.5 x 0.5 in => 0.5 in * 240 dpi = 120x120 px xhdpi -> 320 dpi => 0.5 x 0.5 in => 0.5 in * 320 dpi = 160x160 px xxhdpi -> 480 dpi => 0.5 x 0.5 in => 0.5 in * 480 dpi = 240x240 px xxxhdpi -> 640 dpi => 0.5 x 0.5 in => 0.5 in * 640 dpi = 320x320 px 
  • px – 一个像素,与CSS,JavaScript等中使用的相同
  • 与尺度无关的像素
  • 浸 – 密度独立的像素

通常sp用于字体大小,dip用于(也称为dp)。

以下是Android使用的公式:

px = dp *(dpi / 160)

其中dpi是以下屏幕密度之一。 有关所有可能密度的列表,请访问http://developer.android.com/reference/android/util/DisplayMetrics.html 。 它定义了“DENSITY_ *”常量。

ldpi(低)〜120dpi mdpi(中)〜160dpi hdpi(高)〜240dpi xhdpi(超高)〜320dpi xxhdpi(超高额)〜480dpi xxxhdpi(超高额外)〜640dpi

取自http://developer.android.com/guide/practices/screens_support.html

如果你知道你的屏幕dpi,这将解决很多在px和dp之间转换的困惑。

所以,假设你想要一个60dp的图像作为hdpi屏幕,那么60dp的物理像素大小是:

px = 60 *(240/160)

Android 屏幕大小分为smallmediumlargeextra largedouble-extratriple-extra类别。 屏幕密度是屏幕区域(如英寸)内像素的数量。 一般来说,它是以每英寸点数(dpi)来衡量的。 屏幕密度分为低,中,高和特高。 分辨率是屏幕中的像素总数。

  • dp:密度无关像素,根据屏幕密度而变化。 在160 dpi的屏幕上,1 dp = 1像素。 除了字体大小,总是使用dp。
  • dip: dip == dp。 在早期的Android版本中使用dip,后来改为dp。
  • sp:缩放独立像素,根据用户的字体大小偏好进行缩放。 字体应该使用sp。
  • px:我们常用的映射到屏幕像素的标准像素。
  • in:英寸,相对于物理屏幕尺寸。
  • 毫米:毫米,相对于物理屏幕尺寸。
  • 磅: 1/72英寸,相对于物理屏幕尺寸。

单位换算公式

  px = dp * (dpi / 160) 

设备中的dp到px

下面的例子可能有助于更好地理解 缩放基于120(ldpi),160(mdpi),240(hdpi),320(xhdpi),480(xxhdpi)和640(xxxhdpi)的桶大小发生。 Google对于ldpi:mdpi:hdpi:xhdpi:xxhdpi的build议比例是3:4:6:8:12

一个150像素×150像素将占据,

  • mdpi中150dp X 150dp的屏幕空间
  • hdpi中100dp X 100dp的屏幕空间
  • xhdpi中75dp X 75dp的屏幕空间

如果您希望在所有Android设备上使用统一的UIdevise,则可以使用以下DPI计算器来修复图像大小和其他尺寸。

DPI计算器在Java中

 /* Program output LDPI: 165.0 X 60.0 MDPI: 220.0 X 80.0 HDPI: 330.0 X 120.0 XHDPI: 440.0 X 160.0 XXHDPI: 660.0 X 240.0 XXXHDPI: 880.0 X 320.0 */ public class DPICalculator { private final float LDPI = 120; private final float MDPI = 160; private final float HDPI = 240; private final float XHDPI = 320; private final float XXHDPI = 480; private final float XXXHDPI = 640; private float forDeviceDensity; private float width; private float height; public DPICalculator(){ } public DPICalculator(float forDeviceDensity, float width, float height){ this.forDeviceDensity = forDeviceDensity; this.width = width; this.height = height; } public static void main(String... args) { DPICalculator dpiCalculator = new DPICalculator(240,330,120); dpiCalculator.calculateDPI(); } private float getPx(float dp, float value) { float px = dp * (value / forDeviceDensity ); return px; } private void calculateDPI() { float ldpiW = getPx(LDPI,width); float ldpiH = getPx(LDPI,height); float mdpiW = getPx(MDPI,width); float mdpiH = getPx(MDPI,height); float hdpiW = getPx(HDPI,width); float hdpiH = getPx(HDPI,height); float xdpiW = getPx(XHDPI,width); float xdpiH = getPx(XHDPI,height); float xxdpiW = getPx(XXHDPI,width); float xxdpiH = getPx(XXHDPI,height); float xxxdpiW = getPx(XXXHDPI,width); float xxxdpiH = getPx(XXXHDPI,height); System.out.println("LDPI: " + ldpiW + " X " + ldpiH); System.out.println("MDPI: " + mdpiW + " X " + mdpiH); System.out.println("HDPI: " + hdpiW + " X " + hdpiH); System.out.println("XHDPI: " + xdpiW + " X " + xdpiH); System.out.println("XXHDPI: " + xxdpiW + " X " + xxdpiH); System.out.println("XXXHDPI: " + xxxdpiW + " X " + xxxdpiH); } } 

More Information refer following link.

http://javapapers.com/android/difference-between-dp-dip-sp-px-in-mm-pt-in-android/

sp = scale independent pixel

dp = density independent pixels

dpi = density pixels

I have gone through the above answers…not finding them exactly correct. sp for text size, dp for layout bounds – standard. But sp for text size will break the layout if used carelessly in most of the devices.

sp take the textsize of the device, whereas dp take that of device density standard( never change in a device) Say 100sp text can occupies 80% of screen or 100% of screen depending on the font size set in device

在这里输入图像描述

You can use sp for layout bounds also, it will work 🙂 No standard app use sp for whole text

Use sp and dp for text size considering UX.

  • Dont use sp for text in toolbar( can use android dimens available or dp)
  • Dont use sp for text in small bounded buttons, very smaller text, etc

Some people use huge FONT in their phone for more readability, giving them small hardcoded sized text will be an UX issue. Put sp for text where necessary, but make sure it won't break the layout.

I've come across a good article about designing Android apps UI for different screen resolutions, and I'd like to leave it here just for somebody searching in this area. Yes, I know that it's somehow described in Google docs (and mentioned in the posts above), I read that but it was not good for me (yeah, I may be too stupid)). It remained unclear for me how to design layouts capable to handle different screen size. I hate DP concept and so on, when I need to implement a "flexible" UI layout for different screens. (Hey iOS developers – yes, you're right it's Storyboard concept).

Android has not bad UI concept, but lacks iOS Storyboard features, unfortunately. Designing flexible UI in Android is not easy thing (at the best).

Here goes the article that helped me to understand what to do in Android to make layouts for different screen sizes:

http://jmsliu.com/1718/decide-android-app-screen-size.html

How to Design UI for Android Apps for Different Screen Size

To design an app UI for different screen sizes, our initial design has to meet a minimum required space for each screen size. Android defines a minimum size (in dp) for each generalized screen type. Here is an Android screen size guideline. Minimum Screen Size for Android in dp When we get the screen size in dp, it is not enough for us to design the Android app UI. For each screen size, we need to prepare graphics and bitmap images for each density. Here is an Android screen density guideline. Android Density Guideline (dpi)

For easy calculation, we can follow the 3:4:6:8 scaling ratio between the four generalized densities. If we create a 36×36 pixel picture for ldpi device, the rest densities pictures size will be 48×48 for mdpi, 72×72 for hdpi, and 96×96 for xhdpi.

How to Design Android Apps UI in Photoshop

Many designers have problems for designing Android app UI in photoshop or other pixel based graphic design tools because of density-independent unit, dp. Designers don't know how to map dp to pixel. Google also doesn't give a clear Android UI design guide for them, though they give a basic formula for dp and pixel translation.

As Android's definition, 1pd equal to 1px under 160 dpi device (mdpi). So we want to design an Android app for xlarge Android device with mdpi density, we can define our UI size in pixel as 960 pixel in width and 720px in height; Follow the same mapping rule, we can get following Android App screen size UI design guideline:

Android App Screen Size in Pixel Guideline

ADDED : If you interested in "flexible" UI too, have a look at this library: An Android SDK that provides a new size unit – sdp (scalable dp). This size unit scales with the screen size (this also mentioned in an answer here, about SDP library)

ADDED2 Google has finally understood usefulness of iOS Storeboard UI concept, and here goes ConstraintLayout for Android world: Build a Responsive UI with ConstraintLayout (IMHO a must to read for every Android dev).

Screen size in Android is grouped into categories ldpi , mdpi , hdpi , xhdpi , xxhdpi and xxxhdpi . Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch ( dpi ).

PX(Pixels):

  • our usual standard pixel which maps to the screen pixel. px is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. Not recommended.

DP/DIP(Density pixels / Density independent pixels):

  • dip == dp . In earlier Android versions dip was used and later changed to dp . This is alternative of px .

  • Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size.

  • dp is highly recommended to use in place of px . Use dp if you want to mention width and height to grow & shrink dynamically based on screen sizes.

  • if we give dp/dip , android will automatically calculate the pixel size on the basis of 160 pixel sized screen.

SP(Scale independent pixels):

  • scaled based on user's font size preference. Fonts should use sp .

  • when mentioning the font sizes to fit for various screen sizes, use sp . This is similar to dp .Use sp especially for font sizes to grow & shrink dynamically based on screen sizes

Android Documentation says:

when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi . An sp is the same base unit, but is scaled by the user's preferred text size (it's a scale-independent pixel), so you should use this measurement unit when defining text size

dp: (density independent pixels) The number of pixels represented in one unit of dp will increase as the screen resolution increases (when you have more dots/pixels per inch). Conversely on devices with lower resolution, the number of pixels represented in on unit of dp will decrease. Since this is a relative unit, it needs to have a baseline to be compared with. This baseline is a 160 dpi screen. This is the equation: px = dp * (dpi / 160).

sp: (scale independent pixels) This unit scales according to the screen dpi (similar to dp) as well as the user's font size preference.

px (Pixels) – Actual pixels or dots on the screen.

For more details you can visit

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension And http://developer.android.com/about/dashboards/index.html#Screens

The screen of a mobile phone is made up of thousands of tiny dots known as pixels (px) . A pixel is the smallest element which goes to make the picture. The more the number of pixels to make a picture or wording, the sharper it becomes and makes the smartphone screen more easily readable.

Screen resolution is measured in terms of number of pixels on the screen. Screen resolution is a commonly-used specification when buying a device, but it's actually not that useful when designing for Android because thinking of screens in terms of pixels ignores the notion of physical size, which for a touch device is really really important.

Density independent pixel (dp or dip) allow the designer to create assets that appear in a expected way, no matter the resolution or density of target device.

A density independent pixel (dp or dip) is equal to one pixel at the baseline density or 160 dpi (dots per inch).

1 px/1dp = 160 dpi/160 dpi

2 px/1dp = 320 dpi(2x)/160 dpi

哪里,

dpi is dots per inch

So, at 320 dpi, 1 dp is equal to 2 px.

px/dp = dpi/160dpi

Dots per inch (dpi) is a measure of the sharpness (that is, the density of illuminated points) on a display screen. The dots per inch for a given picture resolution will differ based on the overall screen size since the same number of pixels are being spread out over a different space.

Working with density independent pixels help us to deal with a situation like where you have two devices with same pixel resolution, but differing amount of space. Suppose in a case, a tablet and phone has the same pixel resolution 1280 by 800 pixels (160 dpi) and 800 by 1280 pixels (320 dpi) respectively.

Now because a tablet is at baseline density (160 dpi) its physical and density independent pixels sizes are the same, 1280 by 800. The phone on the other hand has a higher pixel density, so it has half as many density independent pixels as physical pixels. So a phone has 400 by 640 density independent pixels. So using a density-independent pixel makes it easier to mentally picture that tablet has much more space than the phone.

Similarly, if you have two devices with similar screen size, but different pixel density, say one is 800 by 1280 pixels (320 dpi), and the other is 400 by 640 pixels (160 dpi), we don't need to define totally different layouts for these two devices as we can measure assets in terms of density independent pixel which is same for both devices.

800 by 1280 pixels (320dpi)=400 by 640 density independent pixel (dp)

400 by 640 pixels (160 dpi)=400 by 640 density independent pixel (dp)

Scale independent pixels(sp) is the preferred unit for font size. For accessibility purposes, Android allows users to customize their device's font size. Users that have trouble reading text can increase their device's font size. You can normally find this option in the display setting on your phone or tablet under font size. It's often also available through the accessibility settings.

With scale independent pixels, 16 sp is exactly the same as 16 dp when the device's font size is normal or 100%. But when device's font size is large, for example 125%, 16 sp will translate to 20 dp or 1.25 times 16.

If you use dp as the unit for font size, then that piece of text has a specific physical size no matter if the user has customize device's font size. Using sp units will make a better experience for people with impaired eyesight.

Reference : Udacity , Google

sp: scale independent pixel. You should use it with texts because it is automatically scaled according to the font size that is being used by the user in his device. px: pixel or picture element is the single point on the screen

SDP – a scalable size unit – basically it is not a unit, but dimension resources for different screen size.

Try the sdp library from Intuit. It's very handy to solve unit problems, and you can quickly support multiple screens .

用法

android:paddingBottom="@dimen/_15sdp" for positive and android:layout_marginTop="@dimen/_minus10sdp" for negative sdp sdp

It has equivalent value in dp for each size in values-sw<N>dp folders (sw = smallestWidth).

注意

小心使用它! In most cases you still need to design a different layout for tablets.

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_minus10sdp" android:paddingBottom="@dimen/_15sdp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="♡" android:textColor="#ED6C27" android:textSize="@dimen/_70sdp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="U" android:textColor="@android:color/black" android:textSize="@dimen/_70sdp" /> </LinearLayout> 

You can use db for text size, but I prefer ssp for text size.

For more details, check the library GitHub page .

I want to provide an easy way to understand dp . In fact, I think dp is the easiest one to understand. dp is just a physical length unit. It's of the same dimension as mm or inch . It's just convenient for us to write 50dp , 60dp rather than 50/160 inch or 60/160 inch , because one dp is just 1/160 inch whatever the screen size or resolution is.

The only problem is that, the android dpi of some screens are not accurate. For example, a screen classified to 160dpi may have 170dpi indeed. So the computation result of dp is fuzzy. It should be approximately the same as 1/160 inch .

The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.

Note : The compiler accepts both " dip " and " dp ", though " dp " is more consistent with " sp ".

Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user's font size preference.