oldergod oldergod - 15 days ago 10
Android Question

How to mimick Weighted LinearLayout with a Constraint Layout

How can we spare space equally in a Constraint Layout as in LinearLayout?

For instance, how would the below layout become if it was written with constraints?

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="A"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

<TextView
android:id="B"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

<TextView
android:id="C"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

<TextView
android:id="D"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>


In a Constraint Layout, I could set
A
and
D
to the edges,
A←B→D
with a 33 bias and
A←C→D
with a 66 bias to kind of have equal space between each element.

That solution does not really scale though.

Is there a proper way to do this in Constraint Layout?

Answer

FYI -- Constraint Layout alpha 9 added Chains, which allow you to implement this behavior.

enter image description here