你需要使用 “MeasureSpec.makeMeasureSpec” 方法以及 “MeasureSpec.UNSPECIFIED” 值吗?

huangapple go评论60阅读模式
英文:

Do I need to use "MeasureSpec.makeMeasureSpec" with "MeasureSpec.UNSPECIFIED" value?

问题

  1. 2个 measure 函数调用之间有差异吗?

  2. view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)

  3. view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))

  4. 在调试器中,我看到 MeasureSpec.UNSPECIFIED 等于 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),但我不确定是否适用于所有 Android SDK 版本。

  5. 传递参数给 measure 函数的更常规方法是什么?

英文:

Is there any difference between 2 measure function calls?

  1. view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
  2. 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.

huangapple
  • 本文由 发表于 2023年6月30日 03:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定