2010-07-08 21 views
19

Cuando se hace clic en un botón, se ejecuta el siguiente método:agregar dinámicamente 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 es un TableLayout XML. Puedo ver desde la depuración que se está accediendo al método, pero no se dibuja nada en la pantalla. ¿Lo que da? ¿Debo reiniciar la Vista de contenido de alguna manera?

Respuesta

27

Como resultado, Eclipse no siempre tiene la razón. Ctrl + Shift + M hizo la importación incorrecta. Tenía import android.view.ViewGroup.LayoutParams cuando debería tener import android.widget.TableRow.LayoutParams

Todo funciona bien ahora.

+0

¿Podemos establecer la alineación, el margen y la ubicación de estas vistas. –

Cuestiones relacionadas