Vote count:
0
I want a 3x3 grid of views like this:
0 1 2
3 4 5
6 7 8
So I used the following code:
for (int i=0; i<9; i++) {
View ns = (View)View.inflate(ctx, R.layout.view_layout, null);
viewArray.add(ns);
ns.setId(i);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i>2) {
if (i<6) {
lp.addRule(RelativeLayout.BELOW, viewArray.get(1).getId());
}
else {
lp.addRule(RelativeLayout.BELOW, viewArray.get(3).getId());
}
}
if (i%3!=0) {
lp.addRule(RelativeLayout.RIGHT_OF, viewArray.get(viewArray.size()-2).getId());
}
//This shouldn't be necessary, but it is
if (i==3) {
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.RIGHT_OF, viewArray.get(3).getId());
viewArray.get(1).setLayoutParams(lp1);
}
addView(ns, lp);
}
This code currently works correctly, but only because of the "if (i==3)" section. Without it, the 1 overlaps the 0 (so the RIGHT_OF isn't working correctly). Any idea what the problem is?
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire