Android Documentation says视图有两种尺寸,测量尺寸和图纸尺寸。测量的尺寸是在测量通过(onMeasure方法)中计算的尺寸,而图纸尺寸是屏幕上的实际尺寸。特别是,文档说:
这些值可能(但不一定)与测量的宽度和高度不同。
所以,我的问题是:什么可以使绘图尺寸与测量尺寸不同?如果onMeasure(int,int)方法遵循布局要求(作为参数 widthMeasureSpec 和 heightMeasureSpec ),SDK如何确定视图应具有不同的图纸大小?
此外,Android Source Code测量宽度/高度的方式/位置用于计算图纸宽度/高度?我试着查看View source code,但我无法弄清楚measureWidth / Height是如何用来计算最终宽度/高度的。也许它与填充有关,但我不确定。
答案 0 :(得分:80)
顾名思义,在测量和布局阶段使用measuredWidth / height。
让我举个例子,
小部件被要求自己测量,小部件说它想要200px到200px。这是measuredWidth / height。
在布局阶段,即在onLayout方法中。该方法可以使用其子节点的measuredWidth / height或通过调用视图的布局方法来指定新的宽度/高度。
让我们说onLayout方法现在调用childview.layout(0,0,150,150),现在视图的宽度/高度与测量的宽度/高度不同。
我建议不要在onLayout方法之外使用measuredWidth / height。
总结一下。
additionallly
public void View.layout(int l, int t, int r, int b)
似乎是位置和大小分配发生的地方。