在我的Android应用程序中,我想制作一个5x5网格的gridlayout。任何行/列之间没有间距。网格应与父宽度/高度匹配,边距为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);
}
}
}
有谁知道什么是错的?
由于