Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout: square views, evenly distributed, autosized

How can I use ConstraintLayout to get 5 evenly distributed squares, with flexible size depending on screen width?

enter image description here

like image 269
Alexey Avatar asked Sep 23 '17 20:09

Alexey


People also ask

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

Is ConstraintLayout a view?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Can we use linear layout in ConstraintLayout?

You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.


1 Answers

You need to create a chain of widgets that is connected on all sides of its parent container (you set the dimensions of this parent container to whatever you want/need). Each widget should have the app:layout_constraintDimensionRatio="1:1" constraint to ensure a square pattern. For instance:

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

<FrameLayout
    android:id="@+id/frame1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/frame2"
    app:layout_constraintTop_toTopOf="parent"
    />

<FrameLayout
    android:id="@+id/frame2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/frame1"
    app:layout_constraintRight_toLeftOf="@+id/frame3"
    app:layout_constraintTop_toTopOf="parent"
    />

<FrameLayout
    android:id="@+id/frame3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/frame2"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

</android.support.constraint.ConstraintLayout>
like image 175
ema3272 Avatar answered Oct 13 '22 23:10

ema3272