Before you share a lot of interview questions, Manduo comes with the correct answer to a few series of error-prone, we wake up with God.
Today it simple appetizer.
This knowledge, I do not answer ** defined as extra points during the interview, got it wrong topic points, ** but in my experience in previous interview, said that not much can complete up students.
We take a look at all the mastery of the knowledge of it.
Inside early blog, a lot of the time, see the following description:
- If your
View
setmatch_parent
is in theonMeasure
measurement mode it was as follows:EXACTLY
; - If set
wrap_conent
, then the corresponding measurement mode is:AT_MOST
; - A left
UNSPECIFIED
we do not control, is not used;
The foregoing description of every word can be considered wrong.
So today I want to clear up some questions:
match_parent / wrap_conent
Necessarily correspond toEXACTLY/ AT_MOST
it?- Measurement mode in the end is determined by which factors?
UNSPECIFIED
It really unusual?
1. match_parent和wrap_content
it must correspond MeasureSpec.EXACTLY
and MeasureSpec.AT_MOST
do?
surely not.
why?
Because View
at measure
the time, its width and height MeasureSpec
is completely dependent on the parent container, the parent container preaching is what is what it received.
If the parent container onMeasure
method written inside each child died View
of MeasureSpec
a Mode
is UNSPECIFIED
, then, whether you are in xml
the layout or LayoutParams
in how to set the width and height are good, the final child View
of onMeasure
is received UNSPECIFIED
.
Well, not intentionally manually specified.
Take a normal angle of view:
We all know that the custom ViewGroup
process, the need in onMeasure
which sub- View
measure.
The measurement sub- View
time, often by measureChild
, measureChildWithMargins
the method is done (for example FrameLayout
, LinearLayout
, CoordinatorLayout
, ViewPager2
).
Or call the ViewGroup
static method getChildMeasureSpec
to directly obtain the target child View
's MeasureSpec
, and then manually measure
(such as ScrollView
, NestedScrollView
, DrawerLayout
, TabLayout
, ConstraintLayout
).
In fact, measureChild
and measureChildWithMargins
which also will pass getChildMeasureSpec
method to obtain MeasureSpec
, that is, these containers mentioned above, in the measurement of their child View
before, are the first through getChildMeasureSpec
to the sub-method for obtaining View
width and height MeasureSpec
, and then passed to the child View
in the measure
process.
Well, we now take a look at getChildMeasureSpec
a method which does:
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
......
switch (specMode) {
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
......
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == ViewGroup.LayoutParams.MATCH_PARENT) {
......
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == ViewGroup.LayoutParams.WRAP_CONTENT) {
......
resultMode = MeasureSpec.AT_MOST;
}
break;
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
......
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == ViewGroup.LayoutParams.MATCH_PARENT) {
......
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == ViewGroup.LayoutParams.WRAP_CONTENT) {
......
resultMode = MeasureSpec.AT_MOST;
}
break;
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
......
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == ViewGroup.LayoutParams.MATCH_PARENT) {
......
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == ViewGroup.LayoutParams.WRAP_CONTENT) {
......
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
can be seen:
The parent container ** specMode为EXACTLY
** When all normal (child View
size is designated match_parent
or the exact dimen
value, Mode = EXACTLY
the size is specified wrap_content
is Mode = AT_MOST
);
When the parent container ** specMode为AT_MOST
** time, Oh, can be seen, in addition to specifying a dimen
value outside, whether set match_parent
or wrap_content
, Mode
ultimately will become AT_MOST
;
If the parent container ** specMode
is UNSPECIFIED
** so, with the above logic they are similar becomes UNSPECIFIED
, unless the exact specified dimen
value;
Therefore, View
the onMeasure
method receives width and height MeasureSpec
, is not entirely by xml
the width and height of the layout or set LayoutParams
the width and height values determined.
2. What are the factors affecting MeasureSpec of mode?
As can be seen just getChildMeasureSpec method, the factors affecting the measurement mode is mainly View mode of the measuring container belongs View.
That is, under normal circumstances (not intentionally set arbitrary), View measurement mode by:
** its own LayoutParams
value ** + set measurement mode parent container determined.
Why everyone says MeasureSpec.UNSPECIFIED
is not common it?
We all feel that this model is not common, probably because at the time of writing layout, View
width and height can only select match_parent
, wrap_content
or directly specify a precise size, relatively speaking, MeasureSpec.UNSPECIFIED
it is not very clear, because in the daily development, such as without customization View
, basically it does not directly come into contact with.
3. MeasureSpec.UNSPECIFIED is not really unusual?
In daily custom View
, I do have to do special treatment rarely specifically for this model, in most cases, would regard it as MeasureSpec.AT_MOST
the same look, like on the most commonly used TextView
, it also does not distinguish between the measurement UNSPECIFIED和AT_MOST
of.
However, although this pattern is less direct contact, but in many scenes, we have to spend unconsciously, such as RecyclerView
the Item
, if Item
the width / height is wrap_content
and scrollable list, then the Item
width / height measurement mode It would be UNSPECIFIED
.
There is NestedScrollView
and ScrollView
because they are self-expansion FrameLayout
, so their child View
when will measure twice, the first measurement, the child View
's heightMeasureSpec
pattern is to write for the dead UNSPECIFIED
of.
We custom ViewGroup
process, if allowed to sub- View
dimensions than the ViewGroup
large, measuring child View
when you can put Mode
designated UNSPECIFIED
.
Well, I hope you thoroughly understand the custom measurement mode of knowledge.
There is also someone sent me a chart and said this figure will be able to understand that, in fact, this figure was a little bit small problem:
I circled the place, this value is not necessarily 0 , but in most cases UNSPECIFIED
this mode may not care about this size
.
At last
All for today, I outline the Android core technology learning, access to relevant content to play with my GitHub: https: //github.com/Meng997998/AndroidJX
For advanced this way, the learning will pay off!
You take your time to invest in learning, it means that you can gain skills, have the opportunity to increase revenue.
Share my Android PDF to learn Daquan
The Android PDF Daquan really learning include all aspects, and contains basic Java knowledge, the basis of Android, Android Advanced extension, and so on collection algorithm
Android PDF learning Daquan concern me personally introduce, or private letter I get
My collection this study, can effectively help you grasp the knowledge points.
In short we are also here to help enhance learning advanced, but also saves you the time to learn online search data can also be shared with close friends studying together