首页 > 解决方案 > 从代码填充 GridLayout 不会给出与 XML 相同的结果

问题描述

我正在尝试在代码中复制以下 XML:

<GridLayout
    android:id="@+id/row2Grid"
    android:layout_row="1"
    android:layout_column="0"
    android:layout_rowWeight="40"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:alignmentMode="alignMargins"
    android:columnCount="3"
    android:columnOrderPreserved="true"
    android:padding="0px"
    android:rowCount="2"
    >

    <!-- Box 1 -->
    <androidx.cardview.widget.CardView
        app:cardBackgroundColor="#00ff00"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:layout_rowWeight="8"
        android:layout_column="0"
        android:layout_columnWeight="6"

        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp">

    </androidx.cardview.widget.CardView>

    <!-- Box 2 -->
    <androidx.cardview.widget.CardView
        app:cardBackgroundColor="#ff0000"
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:layout_rowWeight="8"
        android:layout_column="1"
        android:layout_columnWeight="6"

        android:layout_marginRight="3dp"
        android:layout_marginBottom="3dp"
        app:cardElevation="8dp">

    </androidx.cardview.widget.CardView>

</GridLayout>

上面看起来像这样,这就是我想要的:

XML 结果

首先,带有 id 的 GridLayoutrow2Grid将保留为 XML。但是 Box 1 和 Box 2 不会,所以它们必须由代码生成并row2Grid动态添加

为此,我创建了这个函数,它在 MainActivity 的 onCreate 函数中调用了两次:

@Override protected void onCreate(Bundle savedInstanceState) { ... generateBox("#ff0000"); generateBox("#00ff00"); ... }

public void generateBox(String colorStr) {

    gridLayout = findViewById(R.id.row2Grid);

    int row2children = gridLayout.getChildCount();

    CardView cardView = new CardView(ctx);
    cardView.setBackgroundColor(Color.parseColor(colorStr));
    GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(row2children));
    cardViewLayoutParams.columnSpec = GridLayout.spec(0,8.0f);
    cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);
    cardViewLayoutParams.height = 0;
    cardViewLayoutParams.width= 0;
    cardViewLayoutParams.setMargins(0,0, 12, 34);

    gridLayout.addView(cardView, cardViewLayoutParams);
    gridLayout.invalidate();
    gridLayout.requestLayout();

}

但它不起作用,只会产生以下结果:

代码结果

我究竟做错了什么?

标签: androidgrid-layoutcardview

解决方案


你知道当你为一个问题苦苦挣扎了几个小时,然后当你提出问题时,你就会为自己找到解决方案?

我一发布问题就看到了解决方案。这当然是由于我首先这样做的事实引起的:

GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(row2children));
cardViewLayoutParams.columnSpec = GridLayout.spec(0,8.0f);
cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);

当我创建时cardViewLayoutParams,我在构造函数中指定 cardView 的行和列属性,但在其正下方覆盖它们。将其更改为以下内容:

GridLayout.LayoutParams cardViewLayoutParams = new GridLayout.LayoutParams();
cardViewLayoutParams.columnSpec = GridLayout.spec(row2children,8.0f);
cardViewLayoutParams.rowSpec = GridLayout.spec(0,8.0f);

使一切按预期工作


推荐阅读