Android 上表示像素的度量单位 px, dp, sp, in, mm

本文介绍了Android开发中常用的像素单位,包括px(像素)、dp(设备独立像素,用于实现设备兼容性)、sp(基于字体大小的像素,适用于文本尺寸调整)、in(英寸)和mm(毫米)。通过示例说明了dp相对于px的优势,解释了为何在布局设计中推荐使用dp单位来确保不同密度设备上的一致显示效果。
摘要由CSDN通过智能技术生成

定义于:

 

http://developer.android.com/reference/android/widget/TextView.html#attr_android:width

 

Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

 

px - 像素,可以理解为屏幕上的绝对度量

dp - 设备独立像素,相当于一种相对度量,出于设备兼容性的考虑,使用dp比px有更好的显示效果.见下文:

 

The reason for dip to exist is simple enough. Take for instance the T-Mobile G1. It has a pixel resolution of 320x480 pixels. Now image another device, with the same physical screen size, but more pixels, for instance 640x480. This device would have a higher pixel density than the G1.

—If you specify, in your application, a button with a width of 100 pixels, it will look at lot smaller on the 640x480 device than on the 320x480 device. Now, if you specify the width of the button to be 100 dip, the button will appear to have exactly the same size on the two devices.

 

 

sp - 基于字体大小的固定比例的像素,没有使用过这种度量。 根据文字上的理解,这也是一种相对度量单位,是为了适应不同的字体而设计的(与dp适应不同的设备宽高相对应)

 

in - 英寸,绝对度量单位

 

mm - 千分米,绝对度量单位。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值