Difference between px, dp, dip and sp in Android?

本文详细解释了Android开发中使用的各种UI单位,包括px(像素)、dp/dip(密度独立像素)、sp(比例独立像素)。这些单位如何根据屏幕密度进行转换,并推荐在不同场景下使用合适的单位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • px is one pixel.
  • sp is scale-independent pixels.
  • dip is Density-independent pixels.

You would use

  • sp for font sizes
  • dip for everything else.

    dip==dp

From Android Developers center:


px
Pixels - corresponds to actual pixels on the screen.

in
Inches - based on the physical size of the screen.
1 Inche = 2.54 centimeters

mm
Millimeters - based on the physical size of the screen.

pt
Points - 1/72 of an inch based on the physical size of the screen.

dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. 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".

sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值