2013-12-10 32 views
0

我在將表格添加到android中的表格佈局時遇到了一些問題。在Android中動態添加TableRows不起作用

這裏是我的代碼:

int z = 0; 
     for(String detail: details){ 
      TableRow tr = new TableRow(this); 
      tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

      //odd/even 
      if(z%2 == 0) 
       tr.setBackgroundColor(Color.parseColor("#F5F5F5")); 

      //set detail text 
      TextView detailText = new TextView(this); 
      RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   
      detailText.setTextSize(16.0f); 
      detailText.setTextColor(Color.parseColor("#333333")); 
      detailText.setPadding(20, 10, 20, 10); 
      detailText.setLayoutParams(dtlp); 
      detailText.setText(detail); 
      tr.addView(detailText); 

      detailsTable.addView(tr); 

      ++z; 
     } 

我不能找出問題的所在。細節textview正在設置,但表格將不會顯示。

+0

上面的代碼都可以,你必須使用LayoutParams,也可以試試** detailsTable.addView(tr,new TableLayout.LayoutParams(...,....))** – smkrn110

+0

檢查* * TableLayout ** ** YOUR_LAYOUT.xml **中的高度和寬度,因爲您已將** TableRow寬度設置爲MATCH_PARENT **將其更改爲WRAP_CONTENT。 – smkrn110

回答

0

a。嘗試改變該參照的 「寬度參數」 到 「RelativeLayout.MATCH_PARENT

OR

灣For循環塊之前創建兩個這樣的佈局參數。

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

TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); 

在循環替換

detailText.setLayoutParams(dtlp); 

detailText.setLayoutParams(trps); 

而更換

detailsTable.addView(tr); 

detailsTable.addView(tr,tlps); 

希望這會有所幫助。

+0

現在它工作。非常感謝! – Patricks

相關問題