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正在設置,但表格將不會顯示。
上面的代碼都可以,你必須使用LayoutParams,也可以試試** detailsTable.addView(tr,new TableLayout.LayoutParams(...,....))** – smkrn110
檢查* * TableLayout ** ** YOUR_LAYOUT.xml **中的高度和寬度,因爲您已將** TableRow寬度設置爲MATCH_PARENT **將其更改爲WRAP_CONTENT。 – smkrn110