2011-12-02 19 views
5

Voglio creare tablelayout interno e aggiungere 1 riga con 2 colonne, il codice seguente non mostra nulla, perché?creando dinamicamente tablelayout (android)

Ecco attività principale:

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT); 
    TextView tv1 = new TextView(this); 
    TextView tv2=new TextView(this); 
    tv1.setLayoutParams(params); 
    tv2.setLayoutParams(params); 
    tv1.setText("Hello1!"); 
    tv2.setText("Hello2!"); 
    TableLayout layoutINNER = new TableLayout(this); 
    layoutINNER.setLayoutParams(params); 
    TableRow tr = new TableRow(this); 
    tr.setLayoutParams(params); 
    tr.addView(tv1); 
    tr.addView(tv2); 
    layoutINNER.addView(tr); 
    LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout); 
    main.addView(layoutINNER); 
} 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/android:main_layout" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
</LinearLayout> 

UPDATE:

Bene, finalmente ho risolto questo problema, era solo bisogno di usare TableRow.LayoutParams con TextViews, non LinearLayout.LayoutParams o altro

+0

Hai provato a usare 'TableLayout.LayoutParams' invece per il vostro' TextViews' e il 'TableRow'? – kaspermoerch

+0

ho provato di recente, non funziona troppo – user1049280

+0

Ecco cosa stai cercando: http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside- a-TableLayout-programatically / – OrhanC1

risposta

2

Ecco il codice di lavoro:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.LinearLayout.LayoutParams; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 
    LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT); 
    TextView tv1 = new TextView(this); 
    TextView tv2=new TextView(this); 
    android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT); 
    tv1.setLayoutParams(trparams); 
    tv2.setLayoutParams(trparams); 
    tv1.setText("Hello1!"); 
    tv2.setText("Hello2!"); 
    TableLayout layoutINNER = new TableLayout(this); 
    layoutINNER.setLayoutParams(params); 
    TableRow tr = new TableRow(this); 

    tr.setLayoutParams(params); 
    tr.addView(tv1); 
    tr.addView(tv2); 
    layoutINNER.addView(tr); 
    LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout); 
    main.addView(layoutINNER); 
} 
} 
Problemi correlati