I. First, let's talk about view and viewgroup.
All the UI classes in the Android system are built on the view and viewgroup classes. The child classes of all views become "widgets", and the Child classes of all viewgroups become "layout ". The view and viewgroup adopt a combination design mode to make "part-whole" treat the same. As the top layer of the layout container class, viewgroup can have view and viewgroup in the layout container.
2. What are the meanings of layoutinflater and layoutinflater. Inflate?
Layoutinflater is a class used to instantiate an XML layout file as a view object.
Layoutinflater. infalte (R. layout. Test, null) is used to fill in a new view from the specified XML resource.
3. Relationships among activities, windows, and views
When we run the program, there is a setcontentview () method. The activity is not actually a display view (intuitively it is). In fact, the activity calls the setcontentview () method of phonewindow, then load the view and put the view on this window. The activity is actually initialized with the phonewindow structure. The activity is actually a control unit, that is, a visual human-machine interaction interface.
A metaphor:
Activity is a worker who controls the window. Window is a display screen used to display information. view is the information to be displayed on the display screen, these views are stacked together (via infalte () and addview () and placed on the window display. Layoutinfalter is a tool used to generate a view. The XML layout file is used to generate a view.
Let's talk about the specific execution process in the code.
Setcontentview (R. layout. Main) is actually the following content. (It is more intuitive to comment out the following code)
Getwindow (). setcontentview (layoutinflater. From (this). Inflate (R. layout. Main, null ))
After the program is run, the activity calls setcontentview () of phonewindow to generate a window, and setcontentview is the bottom view. Then, the layoutinflater. infalte () method is used to load the layout to generate a view object, and addview () method is used to add the object to the window (layer by layer)
Therefore, the activity is not a display view, but a view is a real display view.
Note: Only one phonewindow can be initialized when an activity is constructed. In addition, this phonewindow has a "viewroot". This "viewroot" is a live view viewgroup, is the initial view, and then the views are stacked to the viewroot one by one through the addview method. These stacked views are finally placed on the carrier of the window.