2024年3月9日发(作者:)

距离单位

距离单位之Px,dp,sp,控件的外边距与内边距

Px为像素,宽度*高度,即为分辨率。

什么是dpi: Dots Per Inch的缩写。每英寸所打印的点数或线数,用来表示打印机打印分辨率。

dpi是指每英寸的像素,也就是扫描精度。国际上都是计算一平方英寸面积内像素的多少。

dpi越小,扫描的清晰度越低,由于受网络传输速度的影响,web上使用的图片都是72dpi,

但是冲洗照片不能使用这个参数,必须是300dpi或者更高350dpi。

dpi计算公式:

dpi=(√(横向分辨率^2+纵向分辨率^2))/屏幕尺寸

若说该屏幕为4.3英寸,则是此屏幕的对角线长度为4.3英寸。

但是同一此寸的分辨率不一定一样。

dp:设备无关的像素:

换算公式:px=dp*(dpi/160),所以dpi为160 的屏幕上1dp=1px

了解这些是要设计屏幕控件时,根据屏幕大小不一样,像素密度不一样,而自动地去适应同

样的显示效果。

一般将160dp,例如:像素320*480:算出dpi为160,则要显示屏幕中的一半,就是宽度为

160 px,则正好是160 px=160dp*(160/160);像素是640*960,算出dpi为320,则也要显示

屏幕中的一半,宽度就是320 px,正好是320px=160 dp*(320/160)

所以在编程的时候,Android:layout_width=”160 dp”就好了

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:layout_width="160dp"

android:layout_height="wrap_content"

android:background="#FF0033"

android:text="come on momo "

>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:background="#775500"

android:text="HAPPY"/>

Sp:scaled pixels ,用于指定字体大小,当用户改变手机修改字体时,sp也随之改变。

代码:

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="160dp"

android:layout_height="wrap_content"

android:background="#FF0033"

android:textSize="50dp"