Inflate () is used to find a layout defined in XML, but it is only found and hidden, and the display function is not found at the same time. This is a recent project that has made me confused for several days.
On Android, a method similar to inflate () is called findviewbyid (), which can be used sometimes, but is also different.
The difference is:
If you use another layout in your activity, such as the dialog box layout, you need to set the content of other components on this layout, you must use inflate () the method first finds out the layout of the dialog box, and then finds other components above it with findviewbyid. For example:
ViewView1=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 in the layout dialog box.This. findviewbyid (R. Id. dialog_ TV) will certainly report an error.
ViewViewstub= (Viewstub) findviewbyid (R. Id. stubview). Inflate ();
inflate () it can be understood as "implicit property expansion ", hidden display in the view. Before inflate (), only the control is obtained, but no size does not occupy space in the view. Inflate () has a certain size, only out of the hidden state