关于android 中的长度单位 px,dp,dip,pt,sp

laorer 2010-11-08 05:10:41
介绍就不用说了
http://sean.huanglijiang.com/article.asp?id=232
http://ffly.javaeye.com/blog/776171

而且里面有介绍px与dp如何转换,
上述资料说,android推荐使用 dp,sp,但是在代码中却是使用的是 px度量,所以代码中要设置大小的话,还要转换单位,感觉这样很麻烦,为什么不是直接一种屏幕的大小对应一种设置呢?

可能是同时要维护多个应用的问题,但就是感觉不太爽快..
...全文
884 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
laorer 2010-11-12
  • 打赏
  • 举报
回复
如果直接回复6楼的话,我估计我会平分
Sodino 2010-11-12
  • 打赏
  • 举报
回复
唉,看来6楼回复得晚了...
Sodino 2010-11-12
  • 打赏
  • 举报
回复
请注意下面加粗的:

dp
You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens.

px
Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.

android使用的java的设计思路,对屏幕的渲染对象最小单位是pixel,所以这就是代码中设置的数量单位是像素。
Sodino 2010-11-12
  • 打赏
  • 举报
回复
Dimension

A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:

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. 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 the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
px
Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
mm
Millimeters - based on the physical size of the screen.
in
Inches - based on the physical size of the screen.



http://androidappdocs.appspot.com/guide/topics/resources/more-resources.html#Dimension
laorer 2010-11-08
  • 打赏
  • 举报
回复
另外,activity之间传递数据与一个全局类(比如说ApplicationContext类)来存储数据,区别在哪?
全局类可能会持续的占用一段内存,但可以把在必要的时候把相应字段设为null
activity 之间传递数据,还需要一个bundle,需要一些准备感觉有点麻烦
纠结的木棉花 2010-11-08
  • 打赏
  • 举报
回复
个人觉得,这个是为了匹配不同的分辨率弄的.


Dip與屏幕的分辨率無關,與屏幕密度的大小有關.
一般在密度相同的情況下,定義組件的單位可以用dip,這樣可以保證組件在不同分辨率下,它的那個尺寸不會有大的變化.
而對於Density
假設模擬器的屏幕尺寸相同,但是分辨率不同

pixels = dips * (density / 160)
屏幕分辨率相同的情況下:
android默认屏幕为160dpi, resolution 320*480
如果,定义一个button的长度为100px,那么显示出来的长度还是100px。
此时,定义一个button的长度为100dp,

pixels = dips * (density / 160)
假设屏幕为240dpi, 那么dip就是 240/160
此时,定义一个button的长度为100dp, 那么转换成px,它的显示长度就是150px.
如果,定义一个button的长度为100px,那么显示出来的长度还是100px---
注意:但是由於密度大,單位尺寸的像素點多,則分辨率相對變大,所以顯示清晰,則button的尺寸相對變小,所以看起來雖然同是100px,但是尺寸顯得小了!
Theodore_Lin 2010-11-08
  • 打赏
  • 举报
回复
不是很清楚,一般用dip

80,330

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧