Gridlayout with square kids

I am trying to reach GridLayoutwith square children, as you can see from this image.

enter image description here

Thus, basically the view should be adjusted depending on the number of elements without declaring the column size and row size. Is this possible for GridLayout? If not, then forget about this question.

Now let's move on to the square part. I tried to implement this code

public class SquareLinearLayout extends LinearLayout {


    public SquareLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSize == 0 && heightSize == 0) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

            final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
            setMeasuredDimension(minSize, minSize);
            return;
        }

        final int size;
        if (widthSize == 0 || heightSize == 0) {
            size = Math.max(widthSize, heightSize);
        } else {
            size = Math.min(widthSize, heightSize);
        }

        final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(newMeasureSpec, newMeasureSpec);
    }
}

although it makes a square of the child layout, it also takes up the entire width of the screen, pushing other children out of the visible part of the layout.

Do you have any links or do you have code similar to this class but not taking up the full width of the layout? Thanks in advance.

+4
1

, TableLayout Gridlayout.

, , . .

<?xml version="1.0" encoding="utf-8"?>
<com.android.f45tv.view.ui.SquareLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sllContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/llHolder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="2dp"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@color/white"
            android:layout_marginBottom="2dp"
            android:gravity="center_horizontal" />

        <TextView
            android:id="@+id/tvPercent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:textColor="@color/white"
            android:gravity="center" />

        <TextView
            android:id="@+id/tvBPM"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textSize="12sp"
            android:gravity="end" />

        <TextView
            android:id="@+id/tvBPMLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="bpm"
            android:textSize="10sp"
            android:textColor="@color/white"
            android:gravity="end" />

    </LinearLayout>

</com.android.f45tv.view.ui.SquareLinearLayout>

include .

0

Source: https://habr.com/ru/post/1627118/


All Articles