Gridlayout与方形儿童

时间:2016-02-03 09:34:07

标签: android android-gridlayout

我从这张图片中可以看到,我一直试图与正方形儿童达成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);
    }
}

尽管它确实使子布局成为方形,但它也占据了屏幕的整个宽度,将其他孩子推出布局的可视部分。

您是否有任何链接,或者您自己拥有与此类似的代码,但不占用布局的整个宽度?提前谢谢。

1 个答案:

答案 0 :(得分: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标记。

相关问题