GridLayout与正方形子项的布局

4
我一直在尝试实现一个具有方形子项的GridLayout,如您从此图片中所看到的。

enter image description here

因此,视图应根据项目数量进行调整,而无需我声明列大小和行大小。对于GridLayout是否可行?如果不行,请忘记这个问题。

现在让我们来谈谈正方形部分。我尝试了实现以下代码

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);
    }
}

尽管它可以使子布局为正方形,但它也会占据整个屏幕的宽度,将其他子元素推出可视部分的布局。
您是否有类似于此类但不占用布局整个宽度的链接或代码?提前致谢。

https://dev59.com/dGkw5IYBdhLWcg3wQ4Zc#10033481 - Amit Vaghela
1个回答

0

算了,我已经用上面的代码让它工作了,并将项目放置在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标签将其添加到表行中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接