In the development process, sometimes such a control is required, which is invisible under normal circumstances and does not occupy any layout space. It is only displayed under certain circumstances. In this case, we use a common View and setVisibility (View. GONE) can also be implemented, but even if we do not need to display the View, Android will render the View, but will not display it. What if we need a layout instead of a control? So Will Android do more useless work? ViewStub can avoid this problem. By default, ViewStub is not displayed and does not occupy any space of the parent control. When it is set to display or inflate is called, viewStub will be replaced by the layout file we have prepared in advance.
Simple sample code:
This is the xml part, just like a normal control placed in the position you want to place, and android: layout is set to replace the layout shown by ViewStub when you call inflate () or setVisibility, this can be written in another file, which is not rendered by default.
ViewStub stub = stub. inflate ();
When you first need to display the content you want, you need to render it and call inflate (). At this time, your ViewStub will disappear from the layout level, what replaced him was previously defined