6

How to chain multiple chains of different styles (spread,packed..) in constraint layout?

In other words, how to make a chain of group of views which are already in a chain of different style on the same axis (horizontal/vertical)?enter image description here

As depicted in picture:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:id="@+id/view1"
    android:text="VIEW 1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/view2"/>
<Button
    android:id="@+id/view2"
    android:text="VIEW 2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view1"
    app:layout_constraintBottom_toTopOf="@+id/view3"/>

<Button
    android:id="@+id/view3"
    android:text="VIEW 3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view2"
    app:layout_constraintBottom_toTopOf="@+id/view4"/>

<Button
    android:id="@+id/view4"
    android:text="VIEW 4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view3"
    app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

Preview of current xml code:

Manishoaham
  • 601
  • 1
  • 5
  • 14
  • share your code which you tried – Shweta Chauhan Mar 18 '19 at 13:36
  • 1
    @ShwetaChauhan , See edited question with a simple vertical spread chain. Chaining style is to be done in a way upper two views become packed together and below two views become packed together. – Manishoaham Mar 18 '19 at 13:52
  • 1
    Use multiple constraint layout to use that. Put 2 views in one contsraint layout and other 2 view in other constraint layout.After apply spread chain to both constraint layout. Or you want to do this in single layout? – Shweta Chauhan Mar 18 '19 at 14:23
  • Layout within layout specially in case of if parent is a constraint layout should not be a right way to achieve this. Though I have achieved it with two plain linear layouts containing pair of children to be packed, but it's no efficient way. Yes looking do this in single constraint layout. – Manishoaham Mar 18 '19 at 15:32

1 Answers1

1

In order to achieve a "spread chain of packed chains" you first create two separate packed chains in the normal way. Let's call them chain1 and chain2. Then create a <space>. Now connect them like this: chain1 end -> space, space -> chain2 start, chain2 start -> chain1 end.

Here is a more in depth explanation

Michael P
  • 225
  • 3
  • 11