Android – GridLayout with square children
I've been trying to implement GridLayout with square subitems, as you can see from this image
Therefore, basically, the view should be adjusted according to the number of items without declaring column size and row size. Is this possible for GridLayout? If not, forget the problem
Now let's get to the square part. I try 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 does make the sub layout square, it also occupies the entire width of the screen and pushes other children out of the visible part of the layout
Do you have any links, or do you have a code similar to this class, but it doesn't occupy the whole width of the layout? Thank you in advance
resolvent:
Never have I used the same code above to work and put the project in tablelayout instead of GridLayout
This is an example code, if you fall into this situation like me. First, I create a layout for the square
<?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>
Then add it to my main active layout and include an include tag in the table row