The use of android-layoutinflater and inflate methods

Source: Internet
Author: User

Layoutinflater This class is useful in real-world development, and it acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as Button, TextView, and so on) under the XML layout file.
Specific role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;
2, for an already loaded interface, You can use the Activiyt.findviewbyid () method to get the interface elements in it.
Layoutinflater is an abstract class that is declared in the document as follows:
public abstract class Layoutinflater Extends Object    
  Three ways to get Layoutinflater instances:
 1. Layoutinflater inflater = Getlayoutinflater ();  //calls the activity's Getlayoutinflater ()     
 2. Layoutinflater Localinflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);  3. Layoutinflater Inflater = layoutinflater.from (context);     
   
  In fact, these three ways are essentially the same, as you can see from the source code:
Getlayoutinflater ():
Activity The Getlayoutinflater () method is to call Phonewindow's Getlayoutinflater () method and take a look at the source code:
Public Phonewindow (Context context) {           super (context);           Mlayoutinflater = Layoutinflater.from (context);    

It can be seen that it is actually called layoutinflater.from (context).
Layoutinflater.from (context): public static Layoutinflater from (context context) {          Layoutinflater layoutinflater =                  (Layoutinflater) Context.getsystemservice (context.layout_inflater_service);          if (Layoutinflater = = null) {              throw new Assertionerror ("Layoutinflater not Found.")          ;          return layoutinflater;      }  
It can be seen that it actually calls Context.getsystemservice ().
Conclusion: So the final essence of these three methods is all called Context.getsystemservice ().

Inflate method
Through the SDK API documentation, you can know that the method has the following overload forms, the return value is the View object, as follows:
public view inflate (int. resource, ViewGroup root) public     view Inflate (Xmlpullparser parser, ViewGroup root)     Publ IC View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) public     View Inflate (int resource, VIEWGR OUP Root, Boolean attachtoroot)   
1:
Public View Inflate (int resource, ViewGroup root)
The ID of the Resource:view layout
Root: If NULL, this view is used as the root, at which point the other controls in this view can be applied.
If!null, the default layout is used as the root of the view.
2:
Public View Inflate (Xmlpullparser parser, ViewGroup root)
Parser: You need to parse the parsing interface of XML
Root: If NULL, this view is used as the root, at which point the other controls in this view can be applied.
If!null, the default layout is used as the root of the view.
3:
Public View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot)
Parser: You need to parse the XML parsing interface of the view
Root: If NULL, this view is used as the root, at which point the other controls in this view can be applied.
If!null, the default layout is used as the root of the view.
Attachtoroot:
Ture: This parsed XML is also used as the view root
Fase: Default XML as Root view view
4:
Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot)
The ID of the Resource:view layout
Root: If NULL, this view is used as the root, at which point the other controls in this view can be applied.
If!null, the default layout is used as the root of the view.
Attachtoroot:
Ture: This parsed XML is also used as the view root
Fase: Default XML as Root view view

Schematic code:
public view inflate (int. resource, ViewGroup root) public     view Inflate (Xmlpullparser parser, ViewGroup root)     Publ IC View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) public     View Inflate (int resource, VIEWGR OUP Root, Boolean attachtoroot)   

At the same time, let me go to the API to understand the reasons for these four functions!
In the activity:
Do you know that after Setcontentview (new Mysurfaceview (This)), the view control declared in this activity
For example: TextView Why can't reference the control ID in layout file? Can be applied to the junior, but why compile to report null pointer it! Cause: After Setcontentview (new Mysurfaceview (This)), the view becomes the root view, although it can be applied to the textview corresponding ID, but I do not have this object in Mysurfaceview. So I'm going to quote you a blank pointer! Workaround:
View view = Layoutinflater.from (this). Inflate (r.layout.passover, NULL); Note: Each parse will produce a different object
Then you quote no problem, use it freely.
Use of the Inflate method in Android
The inflate () function is to find out a layout of the XML definition, but only to find out and hide, not find the same time and display the function. and Setcontentview () sets the layout to the current screen as activity content, which can be displayed directly.
Setcontentview () Once called, Layout will display the UI immediately, and inflate will only form an object that is implemented as a view class. Use Setcontentview (view) to display it when necessary.
Note: Inflate () or can be understood as "hidden expansion", hidden in view, inflate () just get control, but no size does not occupy space in the view, inflate () after a certain size, just out of the hidden state.
The interface is typically displayed in activity through Setcontentview (), but if the control layout is set to operate in non-activity, it needs to be layoutinflater loaded dynamically.
<textview    android:id= "@+id/tview"    android:layout_width= "fill_parent"    android:layout_height= " Wrap_content "    android:text=" ataaw.com "/><button    android:id=" @+id/button "    android:layout_ Width= "Fill_parent"    android:layout_height= "wrap_content"    android:text= "button"/>
Load the above layout dynamically in the program.
Layoutinflater Flater = Layoutinflater.from (this); View view = Flater.inflate (r.layout.example, NULL);
Gets the controls in the layout.
Button = (button) View.findviewbyid (R.id.button); TextView = (TextView) View.findviewbyid (R.id.tview);
Next combine the source code to say the inflate method two kinds of forms:
1. Dynamically loaded through the inflate () method of the Layoutinflater class. Two ways to get Layoutinflater
A. Obtained through Systemservice
Layoutinflater inflater= (Layoutinflater) Context.getsystemservice (Context.layout_infleater_service);
B. Getting from a given context
public static Layoutinflater from (context context)
C. The difference between the two: actually the same, the source code
public static Layoutinflater from (context context) {        Layoutinflater layoutinflater =                (layoutinflater) Context.getsystemservice (context.layout_inflater_service);        if (Layoutinflater = = null) {            throw new Assertionerror ("Layoutinflater not Found.")        ;        return layoutinflater;}

D. Implementation code:
The first step is to get the Layoutinflater object first.
Layoutinflater Inflater=context.getsystemservice (Context.layout_infleater_service);
The second step is to call the inflate () method to load the layout resource.
View view=inflater.inflate (int resource, viewgroup root);
Parameter 1: Loading error throws an Inflateexception exception to the XML resource file to be loaded.
Parameter 2: The parent layer of the new view, which can be set to null.
In the third step, adding the newly generated view to the desired view, we can get the view MyView in the current acitivty through the Findviewbyid () method, and then add the newly generated view to the MyView.
Myview.add (view, New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent));
Note: You need to specify the view properties.
You can also call the inflater.inflate (int resource, ViewGroup root) method to join the view directly into the parent view.
such as: Inflater.inflate (R.layout.login,myview);
This situation does not control the length and width of the new view. It is recommended to use the first type.
Note: layoutinflater.inflate () converts the layout file to view, which is intended for use by layout inflater. Although layout is also a subclass of view, in Android, if you want to convert the layout of XML into view into a. Java code, you can only pass inflater, not through Findviewbyid ().
The second method: Use the static method of view:
Static View Inflate (context context, int resource, ViewGroup root)
The context of the parameter 1:acitivity or application
Parameter 2: The specified XML layout file
Parameter 3: Specify the parent component.
The same can be done by:
View view=view.inflate (this,r.layout.*,null);
Generate a new view, and then call the Add method to specify the view's properties and join the parent component.
Of course, you can also use View.inflate (This,r.layout.*,myview) to join the parent component directly.
There are two forms of Findviewbyid:
R.LAYOUT.XX is a layout file that references Res/layout/xx.xml (inflate method), r.id.xx is the component that references the layout file, and the component ID is xx (Findviewbyid method). All component IDs can be viewed with r.id.xx, but the components are not available in Layout setcontentview (), and Activity.findviewbyid () will have null pointer exceptions
A. Findviewbyid (int id) in activity
B. Findviewbyid (int id) in View
6. The difference is that Layoutinflater is used to find layouts under the XML layout file and instantiate it! Instead, Findviewbyid () is looking for specific widget controls (such as: Button,textview, etc.) under specific XML.
Android also has a similar function with inflate () called Findviewbyid (), which can sometimes be used, but there are differences,
The difference is:
Findviewbyid () can only find the components in the current layout, that is, Setconentview () in that layout.
If you use other layout in your activity, such as dialog layout, and you want to set the contents of other components on this layout, you must use the inflate () method to find out the layout of the dialog box first, and then use the Findviewbyid () Find the other components above it. For example:
View view1=view.inflate (this,r.layout.dialog_layout,null);
textviewdialogtv= (TextView) View1.findviewbyid (R.ID.DIALOG_TV);
Dialogtv.settext ("ABCD");
Note: R.ID.DIALOG_TV is a component on the layout of the dialog box, and if the direct use of This.findviewbyid (R.ID.DIALOG_TV) will definitely be an error.

View viewstub = ((viewstub) Findviewbyid (R.id.stubview)). Inflate ();


Welcome to scan QR Code, follow public account



The use of android-layoutinflater and inflate methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.