动态添加textview到表格布局

时间:2011-03-18 16:12:34

标签: android textview tablelayout

下面是我的前端和后端代码。我想要做的是动态地将textview和行添加到xml的表格布局部分。有没有人看错了什么?

<?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);

1 个答案:

答案 0 :(得分:3)

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

相关问题