2011-03-18 299 views
0

嗨下面是我的前端和后端代码。我想要做的是动态地添加一个textview和一行到xml的表格布局部分。有没有人看到任何错误?动态添加textview到表格布局

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout android:id="@+id/relLayout" android:layout_height="wrap_content" 
       android:layout_width="fill_parent"> 
    <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:text="Enter Stock to Add" android:textSize="15px" 
      android:layout_height="wrap_content" android:layout_alignParentTop="true" 
      android:layout_marginLeft="20px" android:layout_marginRight="20px" android:layout_marginTop="20px"></EditText> 
    <Button android:id="@+id/addButton" android:layout_width="100px" 
      android:layout_below="@id/editText1" android:layout_alignParentRight="true" 
      android:layout_height="wrap_content" android:text="Add Stock" android:layout_marginRight="20px" android:textSize="15px"></Button> 

</RelativeLayout> 
<TableLayout android:id="@+id/tableLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent"> 

</TableLayout> 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1); 
    TableRow row = new TableRow(this); 
    TextView tv = new TextView(this); 
    tv.setText("This is text"); 

    tl.addView(row); 
    row.addView(tv); 
    //set the layout of the view 
    setContentView(R.layout.main); 
+0

你没有真正描述什么是_问题_,你为什么认为有什么问题.. – Adinia 2011-03-18 16:16:26

回答

3

做你试图调用的setContentView(R.layout.main);在添加新元素之前?

+0

真棒工作!你知道为什么吗? – locoboy 2011-03-18 16:25:48

+0

我想setContentView调用将从xml描述中生成布局元素。因此,在您修改元素之前,这些修改将会丢失。但那只是我的猜测。 – senola 2011-03-18 16:32:28