I am a bit confused about MeasureSpec.UNSPECIFIED
and MeasureSpec.AT_MOST
.
I know that when match_parent
or an constant dimension valu
The basic definition of how a View is sized goes like this:
MeasureSpec.EXACTLY
- A view should be exactly this many pixels regardless of how big it actually wants to be.
MeasureSpec.AT_MOST
- A view can be this size or smaller if it measures out to be smaller.
MeasureSpec.UNSPECIFIED
- A view can be whatever size it needs to be in order to show the content it needs to show.
MeasureSpec.AT_MOST
will be applied to views that have been set to WRAP_CONTENT
if the parent view is bound in size. For example, your parent View might be bound to the screen size. It's children will be also bound to this size, but it might not be that big. Thus, the parent view will set the MeasureSpec to be AT_MOST
which tells the child that it can be anywhere between 0 and screen size. The child will have to make adjustments to ensure that it fits within the bounds that was provided.
In special cases, the bounds do not matter. For example, a ScrollView
. In the case of a ScrollView
, the height of the child Views are irrelevant. As such, it will supply an UNSPECIFIED
to the children Views which tells the children that they can be as tall as they need to be. The ScrollView
will handle the drawing and placement for them.
A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes:
UNSPECIFIED
The parent has not imposed any constraint on the child. It can be whatever size it wants.
EXACTLY
The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
AT_MOST
The child can be as large as it wants up to the specified size.
MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the tuple into the int