Android 文档说视图有两种尺寸, 测量尺寸和图纸尺寸。测量的尺寸是在测量过程中计算出来的尺寸(onMeasure 方法),而绘图尺寸是屏幕上的实际尺寸。特别是,文档指出:
这些值可能(但不一定)与测量的宽度和高度不同。
那么,我的问题是:什么会导致图纸尺寸与测量尺寸不同?如果 onMeasure(int,int) 方法尊重布局要求(以参数 widthMeasureSpec 和 heightMeasureSpec 的形式给出,SDK 如何决定视图应该具有不同的绘图尺寸?
此外,如何/在哪里在 Android 源代码 测量的宽度/高度用于计算绘图宽度/高度?我试图查看 查看源代码,但我不知道了解如何使用测量的宽度/高度来计算最终的宽度/高度可能与填充有关,但我不确定。
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.
发布评论
评论(1)
顾名思义,测量的宽度/高度在测量和布局阶段使用。
让我举个例子,
一个小部件被要求测量自己,小部件说它想要成为 200px x 200px。这是测量的宽度/高度。
在布局阶段,即在 onLayout 方法中。该方法可以使用其子级的测量宽度/高度,或者通过调用视图的布局方法分配新的宽度/高度。
假设 onLayout 方法调用 childview.layout(0,0,150,150) 现在视图的宽度/高度与测量的宽度/高度不同。
我建议不要在 onLayout 方法之外使用测量的宽度/高度。
总结一下。
另外
public void View.layout(int l, int t, int r, int b)
似乎是位置和大小分配发生的地方。
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 .
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.