Comprehensive analysis of Android view layout process

preface

In the previous article, the author described in detail the first of the three workflow of view, the measure process. If you are not familiar with the measurement process, you can refer to the previous article. The measurement process is mainly to measure the view tree and obtain the measured width and height of each view. With the measured width and height, the layout process is to be carried out. The layout process is much simpler than the measurement process. Then let's start to analyze the layout process in detail.

Layout process of ViewGroup

As mentioned in the previous article, the three processes begin with the viewrootimpl#performtraversals method, in which the measure, layout and draw processes are carried out by calling performmeasure, performlayout and performdraw. Let's start with the performlayout method. Let's look at its source code first:

As can be seen from the above code, the host.layout method of ① is called directly. Host is decorview. For decorview, calling the layout method is to layout itself. Note that the parameters passed are 0, host.getmeasuredwidth, host.getmeasuredheight, which represent the upper, lower, left and right positions of a view respectively. Obviously, The top left position of decorview is 0, and then the width and height is its measured width and height. Since the layout method of view is of final type and subclasses cannot be overridden, we can directly look at the view#layout method:

First, look at the code ①, call the setframe method and pass in the four position information. This method is used to determine the positions of the four vertices of the view, that is, initialize the four values of mleft, mright, MTop and mbottom. After initialization, the layout process of the ViewGroup is completed. Then, let's look at the view#setframe method first:

It can be seen that it initializes the four values of mLeft, mTop, mRight and mBottom. For every View, including ViewGroup, the above four values hold the location information of Viwe, so these four values are the ultimate width height. That is to say, if we want to get the bit information of View, we should call getLeft (), getTop () after the layout method is finished. We can get the final height by other methods. If we call the corresponding method before, we can only get 0 results, so we usually get the View height and width information in the onLayout method.

After setting up the location of ViewGroup, we see that we will then call the second method, onLayout (), which is called in ViewGroup to determine the location of sub View, that is, within the method, sub View will call its own layout method to further complete its own layout process. Because the onmeasure methods of different layout containers have different implementations, it is impossible to explain all layout methods once. In addition, in the previous article, FrameLayout #onmeasure was used to explain, so now we also explain the FrameLayout #onlayout method:

It can be seen from the source code that the onlayout method directly calls the layoutchildren method, and layoutchildren is a specific implementation. First sort out the above logic: first get the padding value of the parent container, and then traverse each child view according to the layout of the child view_ The gravity property, the width of the measurement of the sub View, the padding value of the parent container, and the layout parameters of the stator View are determined. Then the child.layout method is called to transfer the layout process from the parent container to the child element.

Now that we have analyzed the layout process of ViewGroup, we will then analyze the layout process of child elements.

Layout process of sub view

The layout process of the sub view is also very simple. If the sub view is a ViewGroup, the above steps will be repeated. If it is a view, the view#layout method will be called directly. According to the above analysis, the four layout parameters of the view will be set inside the method, and then the onlayout method will be called. Let's take a look at the view#onlayout method:

This is an empty implementation. Its main function is to rewrite this method in our custom view to implement custom layout logic.

So far, the layout process of view has been completely analyzed. It can be seen that the logic of the layout process is much simpler than the measurement process. It is more complex to obtain the measured width and height of a view, while the layout process determines the four position parameters of a view according to the obtained measured width and height. In the next article, we will talk about the last process: drawing process. I hope this article will help you understand the workflow of view. Thank you for reading.

More reading

Android view measurement process (measure) fully parses Android view drawing process (draw) fully parses

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>