Catalin Morosan
Catalin Morosan

Reputation: 7937

Equal spacing between views some of which use percentage of parent on a ConstraintLayout

I have a ConstraintLayout and I am vertically aligning several views. Some of the views use app:layout_constraintHeight_percent to have their height be a percentage of the entire layout/screen and the other views use wrap_content for their height.

What I want is to split the leftover vertical space into equal spacing between each view. Is this possible? Are there any strategies to achieve this?

Here is a sample layout:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF0000"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FFFF00"
        app:layout_constraintHeight_percent="0.2"
        app:layout_constraintTop_toBottomOf="@+id/frame1" />

    <FrameLayout
        android:id="@+id/frame3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF00FF"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintTop_toBottomOf="@+id/frame2" />

    <FrameLayout
        android:id="@+id/frame4"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00FFFF"
        app:layout_constraintTop_toBottomOf="@+id/frame3" />
</android.support.constraint.ConstraintLayout>

Here is how it looks right now:

What I have right now

And what I want to achieve is to have equal white space between each colored rectangle.

Upvotes: 1

Views: 37

Answers (1)

Cheticamp
Cheticamp

Reputation: 62851

You should place the views inside of a vertical chain to spread out the extra space as follows:

enter image description here

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF0000"
        app:layout_constraintBottom_toTopOf="@+id/frame2"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FFFF00"
        app:layout_constraintBottom_toTopOf="@+id/frame3"
        app:layout_constraintHeight_percent="0.2"
        app:layout_constraintTop_toBottomOf="@+id/frame1" />

    <FrameLayout
        android:id="@+id/frame3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF00FF"
        app:layout_constraintBottom_toTopOf="@+id/frame4"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintTop_toBottomOf="@+id/frame2" />

    <FrameLayout
        android:id="@+id/frame4"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#00FFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/frame3" />
</android.support.constraint.ConstraintLayout>

Upvotes: 1

Related Questions