0

TableLayoutを動的に作成しようとしています。3X3の行と列を作成したいと思います。


public class PortAFareActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TableLayout seatTable=new TableLayout(this);        
    ImageButton[][] seatButton=new ImageButton[3][3];
    seatTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    TableRow[] seatRow=new TableRow[3];        
    for (int i = 0; i < 3; i++) {
        seatRow[i]=new TableRow(this);
        seatRow[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));           
        for (int j = 0; j < 3; j++) {               
            seatButton[i][j]=new ImageButton(this);
            seatButton[i][j].setImageResource(R.drawable.seat);             
            seatButton[i][j].setLayoutParams(new LayoutParams(50,30));
            seatRow[i].addView(seatButton[i][j]);
        }   
        seatTable.addView(seatRow[i]);
    }
    LinearLayout lin=(LinearLayout)findViewById(R.id.linLayout);
    lin.addView(seatTable,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    lin.invalidate();
    Toast.makeText(this, " "+seatTable.getChildCount() , Toast.LENGTH_SHORT).show();    
 }
}

私のレイアウトXMLファイルには、idlinLayoutのLinearLayoutが含まれています

どんな助けでもいただければ幸いです。=)

4

1 に答える 1

0

これを試してみてください

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.registration);

    TableLayout seatTable=new TableLayout(this);
    ImageButton[][] seatButton=new ImageButton[3][3];
    seatTable.setBackgroundColor(Color.RED);
    seatTable.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    TableRow[] seatRow=new TableRow[3];
    for (int i = 0; i < 3; i++) {
        seatRow[i]=new TableRow(this);
        seatRow[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        for (int j = 0; j < 3; j++) {
            seatButton[i][j]=new ImageButton(this);
            seatButton[i][j].setImageResource(R.drawable.seat);
            //seatButton[i][j].setLayoutParams(new LayoutParams(50,30));//<--------problem 
            seatRow[i].addView(seatButton[i][j]);
        }
        seatTable.addView(seatRow[i]);
    }
    LinearLayout lin=(LinearLayout)findViewById(R.id.linLayout);
    lin.addView(seatTable);//<-------- not the problem but not required 
    lin.invalidate();
    Toast.makeText(this, " "+seatTable.getChildCount() , Toast.LENGTH_SHORT).show();
 }
于 2012-06-23T18:19:18.430 に答える