Lightweight inertia control-ViewStub

Source: Internet
Author: User

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

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.