2010-08-26 75 views
0

我需要使用代码动态生成表格布局。我无法展示它。我已经将表代生成代码段缩减为其基本操作,试图将它们排除在外,但即使这个大大简化的版本也不起作用(即不显示)。有人能指出我做错了什么吗?谢谢。由代码生成的TableLayout不显示

public class TableByCodeTest extends Activity{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    TableLayout tableLayout = new TableLayout(this); 
    tableLayout.setLayoutParams(
     new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    TableRow firstTableRow = new TableRow(this); 
    firstTableRow.setLayoutParams(
     new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
    TextView title = new TextView(this); 
    title.setText(R.string.title); 
    title.setLayoutParams(
     new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

    firstTableRow.addView(title); 
    tableLayout.addView(firstTableRow); 
    setContentView(tableLayout); 

    } 
} 
+0

使用'hierarchyviewer'检查你的UI来看看是怎么回事错。 – CommonsWare 2010-08-26 12:44:08

回答

2

更改行:

firstTableRow.addView(title); 

firstTableRow.addView(title,new TableRow.LayoutParams(0)); 

将其添加到排0

+0

啊!这样可行。非常感谢! – ianww 2010-08-26 21:21:23

+0

@ Cpt.Ohlund:你救了我好友.. – YuDroid 2012-12-05 14:21:58