FrameLayout
Suitable situations:
- stacking UI elements, e.g., “play” icon on images.
- Fragment containers.
- When only a single view is needed.
like video previews.
Play icon on image and bottom-right icons
1 | <FrameLayout |
LinearLayout
LinearLayout arranges all subviews unidirectionally along horizontal axis or vertical axis.
android:orientationimportant, because it tells whether to align horizontally or vertically.android:layout_weightallows remaining spaces to be allocated to subviews by weights.android:gravitycontrols how all subviews are aligned within theLinearLayoutandroid:layout_gravitycontrols how single subview is aligned within its parent layout.
Typical Scenarios. Navigation bar and tools bar that horizontally arranges multiple buttons and entries; settings page that vertically displays entries; simple tables that vertically displays tags and input box.
Best practices.
- Performance. When
layout_weightsare involved, Android will compute twice. - Combine
layout_weightwith0dp. - Avoid using layout weights in nested
LinearLayout. match_parent’s behaviour inLinearLayout.
RelativeLayout
Best practices.
- Do not make dependency chain too long, i.e.,
A -> B -> C -> D .... As this increases vulnerability and computation complexity. - Performant over
LinearLayout.
ConstraintLayout
Extremely flexible and powerful. Each subview’s position and size can be constrained by other elements, including sibling subviews, parent views, or auxiliary lines.
The constraints can be categorized into:
-
Relative positional constraint
-
Size constraint
-
Bias and baseline constraint
-
Barrier and Guidelines.
-
Chain
-
Group.
Group can group multiple views into one group, to control their visibility as an entire entity. By adding IDs of views to
app:constraint_referenced_ids="id1,id2"to apply.
1 | <androidx.constraintlayout.widget.Group |