Gridlayout动态添加

时间:2024-02-21 08:28:23
//类似数组[行, 列]
//表示起始位置为0,占据2行
GridLayout.Spec rowSpec=GridLayout.spec(0, 2, GridLayout.UNDEFINED);
//表示起始位置为1,占据1列
GridLayout.Spec columnSpec=GridLayout.spec(i, 1, GridLayout.UNDEFINED);
GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec, columnSpec);
gridlayout.addView(view, params);


此外xml中也要预先定好多少行多少列
<android.support.v7.widget.GridLayout
    android:id="@+id/gridlayout"
    app:rowCount="10"
    app:columnCount="6"
    android:background="@mipmap/ic_chat_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.GridLayout>