Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 563
oldergod Avatar asked Jul 26 '16 01:07

oldergod


1 Answers

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

enter image description here

like image 58
Nicolas Roard Avatar answered Oct 12 '22 10:10

Nicolas Roard