ConstraintLayout extension of Views DSL.
Supported platforms: Android.
If you want to use this dependency without using one of the fun packs,
you can use Splitties.viewsDslConstraintlayout, provided you have refreshVersions added to the project.
For reference, the maven coordinates of this module are com.louiscad.splitties:splitties-views-dsl-constraintlayout.
ConstraintLayouttailoredlParamsextensionConstraintLayout.LayoutParamsextensions for safe and readable usage
The lParams() extension function on ConstraintLayout looks to be similar
to the similarly named extensions on LinearLayout and FrameLayout, but
there are two key differences:
- Since
ConstraintLayoutchildren are meant to have constraints, their defaultwidthandheightarematchConstraints, notwrapContent. That means that you'll often have to specifyheight = wrapContentfor things likeTextViews andButtons. - In xml,
match_parentis not supported forConstraintLayoutand can decrease performance (the alternative being adding 0dp + 2 parent relative constraints). This is not the case here asmatchParentis rewritten asmatchConstraintswith the appropriate parent relative constraints. The result is a more readable UI code, without performance compromises.
With this split also comes a set of extension functions to use in
lParams(…) { … }. Almost all of them have optional margin and goneMargin parameters.
Center relatively to parent:
centerHorizontally(…)centerVertically(…)centerInParent(…)
Parent relative constraints:
topOfParent(…)bottomOfParent(…)startOfParent(…)endOfParent(…)leftOfParent(…)rightOfParent(…)
Center relatively to another View:
alignVerticallyOn(…)alignHorizontallyOn(…)centerOn(…)
View relative constraints:
above(…)(alias tobottomToTopOf(…))below(…)(alias totopToBottomOf(…))before(…)(alias toendToStartOf(…))after(…)(alias tostartToEndOf(…))topToTopOf(…)topToBottomOf(…)bottomToTopOf(…)bottomToBottomOf(…)baselineToBaselineOf(…)startToStartOf(…)startToEndOf(…)endToStartOf(…)endToEndOf(…)leftToLeftOf(…)leftToRightOf(…)rightToRightOf(…)rightToLeftOf(…)
Chains:
horizontalChain(…) { … }and its companionhorizontalMarginextension forList<View>.verticalChain(…) { … }and its companionverticalMarginextension forList<View>.
Barriers:
barrier(…)which takes aBarrierType(inline class) and a list or vararg ofViews.startBarrier(…),leftBarrier(…),topBarrier(…),endBarrier(…),rightBarrier(…)andbottomBarrier(…)
Guidelines:
verticalGuideline(…)andhorizontalGuideline(…)which take a begin offset (pixels), an end offset, or a ratio (between 0 and 1).
Groups:
group(…)which takes the views to group.
These methods come with a great bonus feature:
If a View involved in a constraint has no valid id, then a generated one
is automatically assigned to it!
These generated ids can't clash with aapt/xml ids, so it's safe to put
xml defined ids on some views that need to have their state saved (e.g.
a RecyclerView, an EditText or a CheckBox) in the same layout.
Note that View ids are crucial to ConstraintLayout machinery.