我正在尝试创建一个不GridView
使用仅 a的网格RelativeLayout
。问题是根据我的日志,应该正确设置自定义视图(圆圈),但我只看到第一行和第一行。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mainLayout.setLayoutParams(mParams);
itemsLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams iParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
itemsLayout.setLayoutParams(iParams);
grid = new TTTGrid(this, "#ffdd00", _gridCount, _gridSize, false);
TTTItem recent = null;
RelativeLayout.LayoutParams rl;
int k = 0;
for(int i = 0; i < (_gridCount*_gridCount); i++) {
circles[k] = new TTTItem(this, _gridSize, "black");
circles[k].setId((int)k+100);
circles[k].setOnClickListener(itemClickHandler);
circleIDs[k] = circles[k].getId();
rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(recent == null) {
Log.d("DEBUGGY", "" + "recent == null on k: " + k + "/" + circles[k].getId());
rl.addRule(RelativeLayout.BELOW);
}
else {
//if(modulo(k,_gridCount) == 0 && k > 0) {
if(k >= _gridCount) {
Log.d("DEBUGGY", "" + "circle: " + k + "/" + circles[k].getId() + " set below: " + (k-_gridCount) + "/" + circles[k-_gridCount].getId());
rl.addRule(RelativeLayout.BELOW, circles[k-_gridCount].getId());
}
else {
Log.d("DEBUGGY", "" + "circle: " + k + "/" + circles[k].getId() + " set RIGHT OF: " + (k-1) + "/" + circles[k-1].getId());
rl.addRule(RelativeLayout.RIGHT_OF, circles[k-1].getId());
}
//else {
// rl.addRule(RelativeLayout., recent.getId());
//}
}
recent = circles[k];
itemsLayout.addView(circles[k], rl);
k++;
}
Toast.makeText(this, "circles: " + circles.length, Toast.LENGTH_SHORT).show();
mainLayout.addView(grid);
mainLayout.addView(itemsLayout);
setContentView(mainLayout);
}
好的,我将其更改为:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mainLayout.setLayoutParams(mParams);
itemsLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams iParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//iParams.addRule(RelativeLayout.CENTER_IN_PARENT);
itemsLayout.setLayoutParams(iParams);
grid = new TTTGrid(this, "#ffdd00", _gridCount, _gridSize, false);
RelativeLayout.LayoutParams rl;
for(int i=0; i < _gridCount; i++) {
for(int j = 0; j < _gridCount; j++) {
circles[i][j] = new TTTItem(this, _gridSize, "black");
circles[i][j].setId((int)(j*i)+100);
circles[i][j].setOnClickListener(itemClickHandler);
circleIDs[i][j] = circles[i][j].getId();
rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.leftMargin = _gridSize*i;
rl.topMargin = _gridSize*j;
itemsLayout.addView(circles[i][j], rl);
}
}
Toast.makeText(this, "circles: " + circles.length, Toast.LENGTH_SHORT).show();
mainLayout.addView(grid);
mainLayout.addView(itemsLayout);
setContentView(mainLayout);
}