在 Android SDK 中,getWidth/Height()和 getmesuredWidth/Height()之间有什么区别?

一个视图有两种尺寸的 Android 文档说,即 实测尺寸图纸尺寸。测量的尺寸是在 量度通行证(量度方法)中计算出来的,而 图纸尺寸是屏幕上的实际尺寸。特别是,文件表明:

这些值可能(但不一定)不同于测量的宽度和高度。

因此,我的问题是: 是什么使得绘图尺寸与被测尺寸不同?如果 对(int,int)进行测量方法遵守布局要求(作为参数 宽度测量规范高度测量规范给出,SDK 如何决定视图应该有不同的绘图大小?

此外,在 Android 源代码中,如何/在何处使用测量的宽度/高度来计算绘图宽度/高度?我尝试查看 查看源代码,但是我不知道如何使用已测量的宽度/高度来计算最终的宽度/高度。也许和填充物有关,但我不确定。

47626 次浏览

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/height 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/measuredHeight
  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.