我试图创建一个没有GridView
的网格,只使用RelativeLayout
。问题是根据我的日志自定义视图(圆圈)应设置正确,但我只看到第一行和第一行。使用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);
}
*,但我只看到第一行和第一行* - 这意味着什么(我指的是第一行部分)? – Luksprog 2013-02-20 17:57:42
根据我的代码“圈子”应该设置在一个网格..但是当我在我的设备上测试它只显示第一行..和第一列.. – Jakepot 2013-02-21 09:13:52