2010-07-07 97 views
0

我需要仅使用Java代码添加表格行(不是XML设计)。在Android中动态添加表格行?

在该表格行中,我必须添加两个textview字段。我使用下面的代码。

TableRow tblrow=new TableRow(this); 
tblrow.addView(Textview1); 
tblrow.addView(Textview2); 

但它只显示Textview1。如何查看两者?

我是Android新手。

回答

2
TableRow tr1; 
//TableRow tr2;  
TextView txt9; 

TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout); 
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 

LinearLayout.LayoutParams layoutParams = 
    new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT); 

for (int i = 0; i < str_arr.length-1; i++) 
{ 
    string_arr = fun1.split(str_arr[i].trim(),"^"); 
    tr1 = (TableRow) new TableRow(this); 
    txt9=new TextView(this); 
    txt9.setText(strinarr[0]); 
    txt9.setBackgroundColor(intblue); 
    txt9.setTextColor(intwhite); 
    tr1.addView(txt9); 
    tl.addView(tr1,new TableLayout.LayoutParams(layoutParams)); 
}