2012-01-31 11 views
12

Ho questi campi di testo che ho generato dinamicamente. Ma non riesco a impostare parametri di layout per loro. Per favore dimmi cosa sto sbagliando.Come impostare dinamicamente i parametri di layout in Android?

Sono in grado di generare i campi senza parametri di layout. Tuttavia, se utilizzo LayoutParams, non viene nemmeno generato.

Codice:

TableLayout table = (TableLayout) findViewById(R.id.TableLayout1); 

TableRow tr = new TableRow(this); 
LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
tr.setLayoutParams(trparams); 

cg[i] = new EditText(this); 
weight[i] = new EditText(this); 
cg[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
weight[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 

//Here's where it goes wrong. By adding fieldparams, the text field doesn't even get generated. 
LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(10, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); 
tr.addView(cg[i], fieldparams); 
tr.addView(weight[i], fieldparams); 

table.addView(tr); 
+6

È possibile utilizzare linearlayout.layoutparams su una tabella? Shud be tablerow.layoutparams – Amit

+1

Bel commento ... ma piccola correzione Amit.it sarà TableLayout not TableRow – Sameer

risposta

14

Si sta aggiungendo Tavolo riga nella tabella in modo da utilizzare al posto di TableLayout.LayoutParamsLinearLayout.LayoutParams .Come che usiamo LayoutParam secondo il genitore in cui stiamo per aggiungere

6

è necessario utilizzare TableRow.LayoutParams invece di LinearLayout.LayoutParams

Sostituire il codice

LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(10, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); 

con il codice seguente.

TableRow.LayoutParams fieldparams = new TableRow.LayoutParams(10, TableRow.LayoutParams.WRAP_CONTENT, 1.0f); 

È funzionante. Fammi sapere cosa è successo. :-)

Problemi correlati