Skip to content

ConstraintLayout

HarryHaiVn edited this page Aug 24, 2018 · 6 revisions

This document will show you some thing different of ConstraintLayout with other layout (RelativeLayout, LinearLayout...).

Getting Started

ConstraintLayout is allow you to build large and complex layouts with a flat view hierarchy(no nested view groups).It's more flexible than RelaytiveLayout and easier to use with Android Studio's Layout Editor.

*To define a view's position in ConstraintLayout, you need to add at least one horizontal and one vertical for the view. Ex: app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"

*Alignment: With ConstraintLayout .You can align the edge of a view to the same of another view.But One point. It's different.If you want to align a view in the middle of another view.You can create a constraint on both sides. Ex: app:layout_constraintStart_toStartOf="@+id/content" app:layout_constraintEnd_toEndOf="@+id/content"

*Baseline alignment: If you have two TextView and you want to align in a line. ConstraintLayout alows you can align them with Edit Baseline . And click the text baseline and drag the line to another baseline. XMl: app:layout_constraintBaseline_toBaselineOf="@+id/message"

*Guideline: You can add a vertical or horizontal guideline in ConstraintLayout with some dp.The childs view can base margin in it. You can use app:layout_constraintStart_toStartOf="@+id/guideline" app:layout_constraintGuide_percent="0.1"...

*Chains: Chains provide group-like behavior in a single axis (horizontally or vertically). The other axis can be constrained independently Chains has four modes: spread inside, package, spread and weighted. Here is an example image:

Clone this wiki locally