- Mastering Android Studio 3
- Kyle Mew
- 274字
- 2021-07-02 18:42:35
Linear and relative layout classes
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:

The preceding layout was built using only linear layouts, as can be seen from the following 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:
<ImageView
android:id="@+id/image_header_1"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/text_title"
app:srcCompat="@drawable/pizza_01" />
<ImageView
android:id="@+id/image_header_2"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_below="@+id/text_title"
android:layout_toEndOf="@+id/image_header_1"
app:srcCompat="@drawable/pizza_02" />
<ImageView
android:id="@+id/image_header_3"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/text_title"
app:srcCompat="@drawable/pizza_03" />
<ImageView
android:id="@+id/image_header_4"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/text_title"
app:srcCompat="@drawable/pizza_04" />
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.
- 玩轉(zhuǎn)Scratch少兒趣味編程
- Java面向?qū)ο筌浖_發(fā)
- Reactive Programming with Swift
- C語言程序設(shè)計基礎(chǔ)與實驗指導(dǎo)
- 面向STEM的Scratch創(chuàng)新課程
- Mastering Unity Shaders and Effects
- Unity Game Development Scripting
- 精通Python自動化編程
- Tableau 10 Bootcamp
- 微信小程序全棧開發(fā)技術(shù)與實戰(zhàn)(微課版)
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- 硬件產(chǎn)品設(shè)計與開發(fā):從原型到交付
- Scala Functional Programming Patterns
- Visual Basic語言程序設(shè)計基礎(chǔ)(第3版)
- Flink入門與實戰(zhàn)