ConstraintLayout extension of Views DSL.
Supported platforms: Android.
ConstraintLayout
tailoredlParams
extensionConstraintLayout.LayoutParams
extensions for safe and readable usage- Download
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
ConstraintLayout
children are meant to have constraints, their defaultwidth
andheight
arematchConstraints
, notwrapContent
. That means that you'll often have to specifyheight = wrapContent
for things likeTextView
s andButton
s. - In xml,
match_parent
is not supported forConstraintLayout
and can decrease performance (the alternative being adding 0dp + 2 parent relative constraints). This is not the case here asmatchParent
is rewritten asmatchConstraints
with 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 companionhorizontalMargin
extension forList<View>
.verticalChain(…) { … }
and its companionverticalMargin
extension forList<View>
.
Barriers:
barrier(…)
which takes aBarrierType
(inline class) and a list or vararg ofView
s.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.
implementation("com.louiscad.splitties:splitties-views-dsl-constraintlayout:$splitties_version")