What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?

From: http://stackoverflow.com/questions/8657540/what-is-the-difference-between-getwidth-heigth-and-getmeasuredwidth-heigth-i


The Android Documentation says that there is two sizes for a view, the measured dimensions and the drawing dimensions. The measured dimension is the one computed in the measure pass (the onMeasure method), while the drawing dimensions are the actual size on screen. Particularly, the documentation says that:

These values may, but do not have to, be different from the measured width and height.

So, my question is: what could make the drawing dimension be different of the measured dimension? If the onMeasure(int,int) method respects the layout requirements (given as the parameters widthMeasureSpec and heightMeasureSpec, how could the SDK decides that the view should have a different drawing size?

Additionally, how/where in the Android Source Code the measured width/height is used to compute the drawing width/height? I tryed to look into the View source code, but I can't figure out how the measuredWidth/Height is used to compute the final width/height. Maybe it has something to do with the padding, but I'm not sure.

share | improve this question
 
 
I think that these measures can be different in case if a scroll is added to the view. –  Yury Dec 28 '11 at 16:32
 
Maybe, but I developed a custom ViewGroup, and this is happening in my code. I am not dealing with scroll until the point where this behavior occurs. So I think that there should be more than scroll involved into this. –  lgfischer Dec 28 '11 at 18:34
up vote 26 down vote accepted

As the name suggests the measuredWidth/height is used during measuring and layoutting phase.

Let me give an example,

A widget is asked to measure itself, The widget says that it wants to be 200px by 200px. This is measuredWidth/height.

During the layout phase, i.e. in onLayout method. The method can use the measuredWidth/height of its children or assign a new width/width by calling layout method of the view.

lets say the onLayout method calls childview.layout(0,0,150,150) now the width/height of the view is different than the measured width/height.

I would suggest not to use the measuredWidth/height outside onLayout method.

to summarize .

  1. onMeasure -> sets up measuredWidth/height
  2. onLayout -> sets up the width/height of the widget.

additionallly
public void View.layout(int l, int t, int r, int b)
seems to be place where the assignment of position and size happens.

share | improve this answer
 
 
that is , we should always use getWidth/getHeight to get the "real" dimension of a View, right? –  suitianshi Jun 12 at 7:34
 
Yes, but if you are doing your own layouting then you should use measuredWidth instead of width. –  Prakash Nadar Jun 12 at 13:06

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值