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?
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.