Grid 3*3 with tableLayout
I want a 3*3 grid with an ImageButton centered in each cell I've done the code below
TableLayout tl=new TableLayout(this);
int intNbRows=3;
int intNbCols=3;
for (int i = 0 ; i < intNbRows;i++)
{
TableRow tr=new TableRow(this);
tr.setMinimumHeight(hauteur/intNbRows);//TODO : a voir
tr.setMinimumWidth(largeur/intNbCols); //TODO : a voir
tr.setId(100+i);
for (int j = 0 ; j < intNbCols;j++)
{
int numImage=((i*intNbCols)+j+1);
final ImageButton myButton=new ImageButton(this);
Drawable d=Drawable.createFromPath("/sdcard/b" + numImage + ".png");
myButton.setBackgroundDrawable(d);
myButton.setId(200+numImage);
myButton.setOnClickListener(this)开发者_开发技巧;
myButton.setPadding(0, 0, 0, 0);
myButton.setScaleType(ScaleType.CENTER_INSIDE);
tr.addView(myButton);
}
tr.setLayoutParams(new TableRow.LayoutParams(largeur,hauteur/intNbRows));
tl.addView(tr);
}
tl.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
this.setContentView(tl);
With this code, rows have the good height, but not width, and ImageButtons are not centered in cells....
Any idea ?
OK i need to put tablerows in linearlayout, and that works very well :)
精彩评论