1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

android布局单位dip dp sp px

Posted at

android布局常用单位有dip、dp、sp、px。

#dip/dp
表示密度无关像素(density-independent pixel),是基于屏幕物理密度的抽象单元。dp是相对一个160dpi密度的屏幕来讲的,所以在160dpi密度的屏幕里1个dp表示一个像素。屏幕密度高单位dp对应像素值高,屏幕密度低单位dp对应像素值低。也就是说不同密度的屏幕,单位dp对应的像素值也不一样。但记住一点就行,无论哪种密度,1个dp在屏幕上总是表示1/160英寸,从而保证不同屏幕上dp总能表示相同的尺寸。
使用场景:除字体以外任何场景
#sp
表示缩放无关像素(scale-independent pixel),也是一种密度无关像素,只是会受到用户所设定的字体偏好影响,跟随字体大小缩放。
使用场景:只在字体大小
#px
表示像素(pixel),屏幕实际像素。
使用场景:尽量不用,但在准确需要多少像素的场景可以使用,比如:列表分割线高度

下面2张图片可以帮助大家更好的了解上述布局单位

image

image

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?