S
S
SomeDeveloper2021-06-17 09:28:02
XML
SomeDeveloper, 2021-06-17 09:28:02

How to make objects in ConstraintLayout Flow "spread" in Android Studio?

I need to make the objects "spread" in the form (For example, ConstraintLayout Flow, because I didn’t find anything better), there was a Grid, but there the objects depend on each other, i.e. if I change the width of an object, then the width of other objects below or above will also change.

I need that if for some object there was not enough space in the ConstraintLayout Flow, then it occupied the remaining space

. or they will be positioned as if they are trying to stay in the ConstraintLayout Flow
Here is an example

60caea03596e1945992298.png

Here is how objects are laid out if they have enough space

60caea2d16943728039893.png

Moreover, I need to resize these objects in code, not xml.
In the code, I resize objects like this:

View view = (View) findViewById(R.id.item_0);
        view.getLayoutParams().height = tY * 3; // tY - высота * 3, чтобы из px преобразовать в dp
        view.getLayoutParams().width = tX * 3; // tX - ширина


The xml itself

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="#e4e4e4"
        app:constraint_referenced_ids="item_0,item_1,item_2"
        app:flow_horizontalStyle="packed"
        app:flow_wrapMode="aligned"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/item_1"
        android:layout_width="158dp"
        android:layout_height="358dp"
        android:background="#123"
        />


    <View
        android:id="@+id/item_2"
        android:layout_width="122dp"
        android:layout_height="331dp"
        android:background="#f23"
         />

    <View
        android:id="@+id/item_0"
        android:layout_width="117dp"
        android:layout_height="149dp"
        android:background="#a153"
         />

    <TextView
        android:id="@+id/textResult"
        android:layout_width="141dp"
        android:layout_height="60dp"
        app:layout_constraintBottom_toTopOf="@+id/button14"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/flow" />

    <Button
        android:id="@+id/button14"
        android:layout_width="302dp"
        android:layout_height="62dp"
        android:insetTop="6dp"
        android:onClick="OnClick"
        android:text="Назад"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/flow" />

</androidx.constraintlayout.widget.ConstraintLayout>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question