创建gridlayout在android中不起作用

创建gridlayout在android中不起作用,android,android-gridlayout,Android,Android Gridlayout,在我的android应用程序中,我想制作一个5x5网格的网格布局。任何行/列之间没有间距。网格应与具有10dp边距的主宽度/高度相匹配。此外,所有单元格的宽度和高度应相同(即所有宽度相同,所有高度相同,即使宽度不等于高度) 这是我到目前为止所做的,但没有任何结果 public class ColorGrid { private Context context; private RelativeLayout container = null; private GridLa

在我的android应用程序中,我想制作一个5x5网格的网格布局。任何行/列之间没有间距。网格应与具有10dp边距的主宽度/高度相匹配。此外,所有单元格的宽度和高度应相同(即所有宽度相同,所有高度相同,即使宽度不等于高度)

这是我到目前为止所做的,但没有任何结果

public class ColorGrid {

    private Context context;
    private RelativeLayout container = null;
    private GridLayout gridlayout = null;

    public ColorGrid(RelativeLayout container) {
        this.context = container.getContext();
        this.container = container;
        this.gridlayout = createGrid();


        for(int i=0; i<25; i+=1) {
            addCell();
        }

        container.addView(gridlayout);
    }

    private GridLayout createGrid() {
        GridLayout gridview = new GridLayout(context);

        gridview.setColumnCount(5);
        gridview.setRowCount(5);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(10, 10, 10, 10);
        gridview.setLayoutParams(params);

        return gridview;
    }

    private void addCell() {
        View cell = new View(context);
        cell.setBackgroundColor(Color.parseColor("#ffffff"));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 0, 1.0f);
        cell.setLayoutParams(params);

        gridlayout.addView(cell);
    }

    public void dismiss() {
        if (container != null && gridlayout != null) {
            container.removeView(gridlayout);
        }
    }
}
公共类ColorGrid{
私人语境;
私有RelativeLayout容器=null;
private GridLayout GridLayout=null;
公共颜色网格(相对长度容器){
this.context=container.getContext();
this.container=容器;
this.gridlayout=createGrid();
对于(int i=0;i