2010-08-31 57 views
0

我希望有一個3×3格,在每個單元 中心的ImageButton的我已經做了以下網格tableLayout

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); 

代碼有了這個代碼,行有良好的高度,而不是3 * 3寬度和ImageButtons不在單元中居中......

任何想法?

回答

1

好的,我需要把桌布放在linearlayout中,而且工作得很好:)