如何以编程方式将 app:layout_columnWeight 和 app:layout_rowWeight 设置为 GridLayout 的 child

how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically

我的 layout.xml 中有一个 GridLayout。我想将 ImageView 作为 child 添加到 GridLayout。我还需要为 child 设置一些属性。

下面是我想以编程方式执行的 layout.xml

<android.support.v7.widget.GridLayout
                    android:id="@+id/gridLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    app:columnCount="3"
                    app:rowCount="3">

                    <ImageView
                        android:id="@+id/r1c1"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/a270"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r1c2"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/c0"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r1c3"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/a0"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r2c1"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/c270"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r2c2"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/e0"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r2c3"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/c90"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r3c1"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/a180"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r3c2"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/c180"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />

                    <ImageView
                        android:id="@+id/r3c3"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/a90"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"
                        app:layout_rowWeight="1" />
                </android.support.v7.widget.GridLayout>

我以编程方式尝试了这个。但问题是我无法以编程方式设置 app:layout_columnWeightapp:layout_rowWeight

问题是,如何以编程方式设置 app:<property-name>

您可以像这样动态生成图像视图并将其分配给您的网格布局..

    ImageView imageView = new ImageView(MainActivity.this);
    int width = 200;
    int height = 150;
    RelativeLayout relt = new RelativeLayout(MainActivity.this);
    RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width, height);
    RelativeLayout.LayoutParams parms_play = new RelativeLayout.LayoutParams(100, 70);
    imageView.requestLayout();
    imageView.setId(i);
    imageView.setPadding(4, 4, 4, 4);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    relt.addView(imageView);
    gridLayout.addView(relt);

对于网格布局规范,选择

GridLayout gl = (GridLayout) findViewById(R.id.grid_layout);
    gl.setColumnCount(8);
    gl.setRowCount(8);

    for(int i=0; i<gl.getRowCount(); i++)
    {
        GridLayout.Spec rowSpec = GridLayout.spec(i, 1, GridLayout.FILL);

        for(int j=0;j<gl.getColumnCount();j++)
        {
            GridLayout.Spec colSpec = GridLayout.spec(j,1, GridLayout.FILL);

            TextView tvChild = new TextView(this);
            tvChild.setText("[ " + i + " | " + j + " ]");
            tvChild.setTextSize(18f);
            tvChild.setTextColor(Color.WHITE);
            tvChild.setGravity(Gravity.CENTER);

            GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
            myGLP.rowSpec = rowSpec;
            myGLP.columnSpec = colSpec;

            gl.addView(tvChild, myGLP );
        }
    }

有关详细信息,请查看此

GridLayout Specs