Android Abstract layout – detailed explanation of include, merge and viewstub

In the layout optimization, Androi's official mentioned the three layouts < include / >, < merge / >, < viewstub / >, and introduced their respective advantages. The following is also a brief description of their advantages and how to use them. Take it down as a note.

1. Layout reuse < include / >

1) The < include / > tag can use a separate layout attribute, which is also required.

2) Other properties can be used< If the include / > tag specifies the ID attribute and your layout also defines the ID, then your layout ID will be overwritten, which is the solution.

3) All Android: layouts in the include tab_* Both are valid, provided that layout must be written_ Width and layout_ Height has two properties.

4) The layout can contain two identical include labels. The following methods can be used for reference (Reference):

2. Reduce view level < merge / >

< merge / > tag plays a very important role in UI structure optimization. It can delete redundant levels and optimize UI< Merge / > is mostly used to replace FrameLayout, or when one layout contains another, < merge / > tags eliminate redundant view groups in the view hierarchy. For example, your main layout file is a vertical layout, which introduces an include of the vertical layout. If the LinearLayout used by the include layout is meaningless, it will slow down your UI performance. In this case, you can use < merge / > label optimization.

Now, when you add the layout file (using the < include / > tag), the system ignores the < merge / > node and directly adds two buttons. For more < merge / > introduction, please refer to Android layout tricks #3: optimize by merging

3. Use < viewstub / > if necessary

The biggest advantage of < viewstub / > tag is that it will be loaded when you need it. Using it will not affect the performance of UI initialization. The < viewstub / > tag can be used for various uncommon layouts, such as progress bars and error messages, to reduce memory usage and speed up rendering< Viewstub / > is an invisible view with size 0< The viewstub / > tag is used as follows:

When you want to load a layout, you can use one of the following methods:

When the inflate () function is called, the viewstub is replaced by the referenced resource and returns the referenced view. In this way, the program can directly get the referenced view without calling the function findviewbyid () again to find it.

Note: one defect of viewstub is that it does not support < merge / > tags.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>