Learning Android "error-prone" series: old drivers out of the pit, you go in yet?

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 Viewset match_parentis in the onMeasuremeasurement mode it was as follows: EXACTLY;
  • If set wrap_conent, then the corresponding measurement mode is: AT_MOST;
  • A left UNSPECIFIEDwe 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:

  1. match_parent / wrap_conentNecessarily correspond to EXACTLY/ AT_MOSTit?
  2. Measurement mode in the end is determined by which factors?
  3. UNSPECIFIED It really unusual?

1. match_parent和wrap_contentit must correspond MeasureSpec.EXACTLYand MeasureSpec.AT_MOSTdo?

surely not.

why?

Because Viewat measurethe time, its width and height MeasureSpecis completely dependent on the parent container, the parent container preaching is what is what it received.

If the parent container onMeasuremethod written inside each child died Viewof MeasureSpeca Modeis UNSPECIFIED, then, whether you are in xmlthe layout or LayoutParamsin how to set the width and height are good, the final child Viewof onMeasureis received UNSPECIFIED.

Well, not intentionally manually specified.

Take a normal angle of view:

We all know that the custom ViewGroupprocess, the need in onMeasurewhich sub- Viewmeasure.

The measurement sub- Viewtime, often by measureChild, measureChildWithMarginsthe method is done (for example FrameLayout, LinearLayout, CoordinatorLayout, ViewPager2).

Or call the ViewGroupstatic method getChildMeasureSpecto directly obtain the target child View's MeasureSpec, and then manually measure(such as ScrollView, NestedScrollView, DrawerLayout, TabLayout, ConstraintLayout).

In fact, measureChildand measureChildWithMarginswhich also will pass getChildMeasureSpecmethod to obtain MeasureSpec, that is, these containers mentioned above, in the measurement of their child Viewbefore, are the first through getChildMeasureSpecto the sub-method for obtaining Viewwidth and height MeasureSpec, and then passed to the child Viewin the measureprocess.

Well, we now take a look at getChildMeasureSpeca 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 Viewsize is designated match_parentor the exact dimenvalue, Mode = EXACTLYthe size is specified wrap_contentis Mode = AT_MOST);

When the parent container ** specMode为AT_MOST** time, Oh, can be seen, in addition to specifying a dimenvalue outside, whether set match_parentor wrap_content, Modeultimately will become AT_MOST;

If the parent container ** specModeis UNSPECIFIED** so, with the above logic they are similar becomes UNSPECIFIED, unless the exact specified dimenvalue;

Therefore, Viewthe onMeasuremethod receives width and height MeasureSpec, is not entirely by xmlthe width and height of the layout or set LayoutParamsthe 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 LayoutParamsvalue ** + set measurement mode parent container determined.

Why everyone says MeasureSpec.UNSPECIFIEDis not common it?

We all feel that this model is not common, probably because at the time of writing layout, Viewwidth and height can only select match_parent, wrap_contentor directly specify a precise size, relatively speaking, MeasureSpec.UNSPECIFIEDit 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_MOSTthe same look, like on the most commonly used TextView, it also does not distinguish between the measurement UNSPECIFIED和AT_MOSTof.

However, although this pattern is less direct contact, but in many scenes, we have to spend unconsciously, such as RecyclerViewthe Item, if Itemthe width / height is wrap_contentand scrollable list, then the Itemwidth / height measurement mode It would be UNSPECIFIED.

There is NestedScrollViewand ScrollViewbecause they are self-expansion FrameLayout, so their child Viewwhen will measure twice, the first measurement, the child View's heightMeasureSpecpattern is to write for the dead UNSPECIFIEDof.

We custom ViewGroupprocess, if allowed to sub- Viewdimensions than the ViewGrouplarge, measuring child Viewwhen you can put Modedesignated 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 UNSPECIFIEDthis 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

Published 168 original articles · won praise 71 · views 20000 +

Guess you like

Origin blog.csdn.net/Aerfa789/article/details/105227958