2010-07-08 16 views
19

Quando un pulsante viene cliccato, il seguente metodo viene eseguito:aggiungere dinamicamente TableRow a TableLayout

public void createTableRow(View v) { 
    TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet); 
    TableRow tr = new TableRow(this); 
    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    tr.setLayoutParams(lp); 

    TextView tvLeft = new TextView(this); 
    tvLeft.setLayoutParams(lp); 
    tvLeft.setBackgroundColor(Color.WHITE); 
    tvLeft.setText("OMG"); 
    TextView tvCenter = new TextView(this); 
    tvCenter.setLayoutParams(lp); 
    tvCenter.setBackgroundColor(Color.WHITE); 
    tvCenter.setText("It"); 
    TextView tvRight = new TextView(this); 
    tvRight.setLayoutParams(lp); 
    tvRight.setBackgroundColor(Color.WHITE); 
    tvRight.setText("WORKED!!!"); 

    tr.addView(tvLeft); 
    tr.addView(tvCenter); 
    tr.addView(tvRight); 

    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
} 

R.id.spreadsheet è un TableLayout xml. Vedo dal debug che si accede al metodo, ma non viene disegnato nulla sullo schermo. Cosa dà? Devo resettare la vista del contenuto in qualche modo?

risposta

27

Come risulta, Eclipse non ha sempre ragione. Ctrl + Shift + M ha fatto l'importazione sbagliata. C'era import android.view.ViewGroup.LayoutParams quando dovrebbe avere import android.widget.TableRow.LayoutParams

Ora tutto funziona correttamente.

+0

È possibile impostare l'allineamento, il margine e la posizione di queste viste. –

Problemi correlati