The linear layout is relatively lightweight and very useful for layouts based on single rows or columns. However, more complex layouts require nesting layouts inside each other and this very quickly becomes resource hungry. Take a look at the following layout:
Nested linear layouts
The preceding layout was built using only linear layouts, as can be seen from the following Component Tree:
The component tree
Although perfectly workable and easy to understand, this layout is not as efficient as it could be. Even a single extra layer of layout nesting will have an impact on performance. Prior to the constraint layout, this problem was solved with the relative layout.
As the name suggests, the relative layout allows us to place screen components in relation to each other, using markup such as layout_toStartOf or layout_below. This allows us to flatten view hierarchies and the preceding layout could be recreated with just one single relative, root viewgroup. The following code demonstrates how the row of images in the previous layout can be generated without nesting any new layouts:
Even if you are new to Android Studio, it is assumed that you will be familiar with linear and relative layouts. It is less likely that you will have encountered the constraint layout, which has been especially developed for Studio to alleviate the shortcomings of these older approaches.
In the previous examples, we used app:srcCompat as opposed to android:src. This is not strictly required here, but if we wished to apply any tinting to the image and hope to distribute the app for older Android versions, this choice will enable that.