In today's development environment, it is easy to forget to optimize the layout, we just need to adjust the adjustment in the IDE and then Setcontentview (), then let's see what he did in the process:
1. Reading files
2. Parsing data
3. Expand Layout
If the layout structure is complex, the longer it takes, then we might as well write a simple adjustment to the structure:
First, the use of relative layout
As a developer, the first thing we might be exposed to is linearlayout, because he is a default layout, and this layout is easier to use. For a long time we will find that a little copy of the structure with linearlayout words, will be nested deep, hierarchical structure is more complex, then we might as well use relativelayout
Second, the combined layout
Generally for the top level element for framelayout, because the activity of the content view is a framelayout, we might as well use the hierarchyviewer to see, If we just write framelayout, we'll find a framelayout on the top floor. If you replace framelayout with a <merge> expression, you'll find that there's less than one framelayout, which can play a role in reducing the hierarchy.
Third, Reuse layout
When it comes to reusing layouts, of course, it's a <include> tag, and many times with this there are two purposes, 1 are referenced multiple times in a layout, 2 Many files are common to a layout, the included layouts are dynamically processed, and are completed at compile time.
Iv. viewstub
This is a deferred initialization technology, he can not only improve performance but also save memory, some complex layout expansion takes time, but sometimes it is not necessary to expand at this time, we can consider using Viewstub to choose the right time to expand it, he has two ways of expansion
1.viewstub.inflate ()
2. Use Setvisibility () to expand it
Each has its own advantages, which one is suitable for use.
On Android layout optimization