如何动态地向表布局添加新行

时间:2013-11-20 05:33:02

标签: android tablelayout

在我的Android应用程序中,我有一个表格布局。我想动态插入行。但问题是行是水平添加的。我希望每个tablerow垂直堆叠。到目前为止我有这个:

    TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer);

    TableRow tablerow = new TableRow(this);
    tablerow.setOrientation(TableRow.VERTICAL);
    EditText g = new EditText(this);
    g.setText("AAAAAAAAA");
    g.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    EditText g2 = new EditText(this);
    g2.setText("BBBBBBBBBB");
    g2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    tablerow.addView(g);
    tablerow.addView(g2);
    mytable.addView(tablerow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

    TableRow tablerow2 = new TableRow(this);
    tablerow2.setOrientation(TableRow.VERTICAL);
    EditText g4 = new EditText(this);
    g4.setText("AAAAAAAAA");
    g4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    EditText g5 = new EditText(this);
    g5.setText("BBBBBBBBBB");
    g5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    tablerow.addView(g4);
    tablerow.addView(g5);
    mytable.addView(tablerow2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

XML

            <TableLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/studentContainer" >
            </TableLayout >

有谁知道这里有什么不对吗?

由于

2 个答案:

答案 0 :(得分:1)

尝试将第一行放入xml并旋转整个表格,如有必要,请编辑text_views。喜欢这个

XML

<TableLayout
    android:id="@+id/list_tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:rotation="-90"
    android:stretchColumns="0,1,2,3" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Column1"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Column2"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Column3"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Column4"
                android:textColor="#000000"/>

</TableRow>
</TableLayout>

然后像这样的代码

TableRow tr = new TableRow(this);
                tr.setPadding(0,20,0,20);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                TextView tv_item1 = new TextView(this);
                TextView tv_item2 = new TextView(this);
                TextView tv_item3 = new TextView(this);
                TextView tv_item4 = new TextView(this);

                tv_item1.setText(M_Number);
                tv_item1.setGravity(Gravity.CENTER);


                tv_item2.setText(M_Number);
                tv_item2.setGravity(Gravity.CENTER);

                tv_item3.setText(M_Number);
                tv_item3.setGravity(Gravity.CENTER);

                tv_item4.setText(M_Number);
                tv_item4.setGravity(Gravity.CENTER);

                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                tr.addView(tv_item4);

Table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

最好是在XML中有一个表行项目,以新视图的形式膨胀并相应地设置数据。

然后将该视图添加到表格布局