英文:
Do I need to use "MeasureSpec.makeMeasureSpec" with "MeasureSpec.UNSPECIFIED" value?
问题
-
2个
measure
函数调用之间有差异吗? -
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
-
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
-
在调试器中,我看到
MeasureSpec.UNSPECIFIED
等于MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
,但我不确定是否适用于所有 Android SDK 版本。 -
传递参数给
measure
函数的更常规方法是什么?
英文:
Is there any difference between 2 measure
function calls?
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
In the debugger, I saw that the MeasureSpec.UNSPECIFIED
equals to the MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
but I am not sure if it's applicable to all Android SDK versions.
What is the more conventional way to pass an argument to the measure
function?
答案1
得分: 1
View.MeasureSpec#UNSPECIFIED 等于零。在你的示例中,你还编码了零宽度。View.MeasureSpec#makeMeasureSpec 将这两个值编码成一个整数,所以结果是零。(你可以查看 makeMeasureSpec()
的代码,看看是如何完成的。
View#measure(int, int) 调用 View#onMeasure(int, int)。
视图的实际测量工作是在
onMeasure(int, int)
中执行的,由此方法调用。因此,只有onMeasure(int, int)
可以并且必须被子类重写。
onMeasure()
的参数是 widthMeasureSpec:
int: 由父视图强加的水平空间要求。这些要求用 View.MeasureSpec 编码。
heightMeasureSpec int: 由父视图强加的垂直空间要求。这些要求用 View.MeasureSpec 编码。
遵循上面的编码建议,即使宽度/高度不为零,你也将能够进行良好形式的测量调用。
英文:
View.MeasureSpec#UNSPECIFIED is equal to zero. You are also encoding a zero width in your example. View.MeasureSpec#makeMeasureSpec encodes these two values into an int, so the result is zero. (You can look at the makeMeasureSpec()
code to see how this is done.
View#measure(int, int) calls View#onMeasure(int, int).
>The actual measurement work of a view is performed in onMeasure(int, int), called by this method. Therefore, only onMeasure(int, int) can and must be overridden by subclasses.
The arguments for onMeasure()
are widthMeasureSpec:
>int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
>
>heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
Follow the encoding advice above and you will be make well-formed measurements calls even when the width/height is other than zero.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论