Android and androidsdk
In actual development, the LayoutInflater class is very useful, and its function is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files under res/layout/, while findViewById () is to find the specific widget controls (such as Button and TextView) under the xml layout file ).
Specific functions:
1. LayoutInflater. inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded;
2. For a loaded interface, you can use the Activiyt. findViewById () method to obtain the interface elements.
LayoutInflater is an abstract class, which is declared as follows in the document:
Public abstract class LayoutInflater extends Object
Three methods for obtaining a LayoutInflater instance
1.LayoutInflater inflater = getLayoutInflater (); // call the getLayoutInflater () of the Activity ()
2.LayoutInflater localinflater = (LayoutInflater) context. getSystemService
(Context. LAYOUT_INFLATER_SERVICE );
3.LayoutInflater inflater = LayoutInflater. from (context );
In fact, these three methods are essentially the same, as can be seen from the source code:
GetLayoutInflater ():
The getLayoutInflater () method of the Activity is to call the getLayoutInflater () method of PhoneWindow. Let's take a look at the source code:
We can see that it actually calls LayoutInflater. from (context ).
LayoutInflater. from (context ):
- Public StaticLayoutInflater from (Context context ){
- LayoutInflater =
- (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
- If(LayoutInflater =Null){
- Throw NewAssertionError ("LayoutInflater not found .");
- }
- ReturnLayoutInflater;
- }
We can see that it actually calls context. getSystemService ().
Conclusion: The three methods are called Context. getSystemService ().
Inflate Method
Through the sdk api documentation, you can know that this method has the following overload forms, and the returned values are View objects, as follows:
Sample Code:
For the code above, specify the second parameter ViewGroup root. Of course, you can also set it to null.
Note:
· The inflate method is different from the findViewById method;
· Inflater is used to find and instantiate xml layout files under res/layout;
· FindViewById () is used to find widgets (such as buttons and TextView) in a specific xml layout file ).