2012-04-14 39 views
1

我試圖做一個3列TableLayout,但我從來沒有看到任何東西。這是我的Java代碼:內容不顯示在動態創建的Android TableLayout

  TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1); 
     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 


     TextView tv = new TextView(this); 
     tv.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
     tv.setText(data[0]); 
     tr.addView(tv); 

     TextView tv1 = new TextView(this); 
     tv1.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tv1.setText(data[1]); 
     tr.addView(tv1); 

     TextView tv2 = new TextView(this); 
     tv2.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     tv2.setText(data[2]); 
     tr.addView(tv2); 

     /* Add row to TableLayout. */ 
     tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

這裏是我的XML文件:

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



<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="350dp" 
    android:layout_marginTop="10dp" 
    > 

</TableLayout> 


<Button 
    android:id="@+id/BorrarPedido" 
    android:layout_width="204dp" 
    android:layout_height="wrap_content" 
    android:text="Borrar Pedido" 
    android:layout_gravity="center" /> 

<Button 
    android:id="@+id/EnviarPedido" 
    android:layout_width="204dp" 
    android:layout_height="wrap_content" 
    android:text="Enviar Pedido" 
    android:layout_gravity="center" /> 

    </LinearLayout> 

我沒有得到任何的錯誤信息,我什麼也沒得到。問題是,除了TableLayout之外的2 Buttons,我什麼都看不到。任何想法爲什麼發生這種情況

+0

是具有values.Also嘗試設置TextViews。有時的setTextColor你的活動主題會導致混淆 – 2012-04-14 15:44:01

+0

數據有VA數據當然可以。我試圖改變textview的顏色,但仍然沒有任何視圖。 – Aldridge1991 2012-04-14 15:50:47

回答

0

嘗試這樣的:

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

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="350dp" 
     android:layout_marginTop="10dp" android:stretchColumns="*" > 
    </TableLayout> 

    <Button 
     android:id="@+id/BorrarPedido" 
     android:layout_width="204dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Borrar Pedido" /> 

    <Button 
     android:id="@+id/EnviarPedido" 
     android:layout_width="204dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Enviar Pedido" /> 
</LinearLayout> 


    TableLayout tblLayout = (TableLayout)findViewById(R.id.tableLayout1); 

    TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT); 

    TableRow row = new TableRow(this); 
    row.setBackgroundColor(Color.DKGRAY); 
    tblLayout.addView(row, layoutParams); 

    addToTableRow("Name", row, Color.WHITE); 
    addToTableRow("Age", row, Color.WHITE); 
    addToTableRow("Location", row, Color.WHITE); 
    } 

    private void addToTableRow(String str, TableRow row,int color) 
    { 
    TextView t = new TextView(this); 
    t.setTextColor(color); 
    t.setText(str); 
    row.addView(t); 
    } 
+0

確實android:方向如此重要? – deadfish 2012-04-14 19:47:14

3

TextView s爲一個TableRow的孩子,所以你應該設置的TableRow.LayoutParams實例爲LayoutParams,而不是簡單的一個(可能是從ViewGroup超類),您目前使用方法:

//... 
     TextView tv = new TextView(this); 
     tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     tv.setText("text 1"); 
     tr.addView(tv); 

     TextView tv1 = new TextView(this); 
     tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     tv1.setText("text 2"); 
     tr.addView(tv1); 

     TextView tv2 = new TextView(this); 
     tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     tv2.setText("text 3"); 
     tr.addView(tv2); 
//... 
+0

解決了我的類似問題...謝謝... – anni 2013-11-20 23:38:15