Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在表格布局中动态添加新行_Android_Tablelayout - Fatal编程技术网

Android 如何在表格布局中动态添加新行

Android 如何在表格布局中动态添加新行,android,tablelayout,Android,Tablelayout,在我的android应用程序中,我有一个表格布局。我想动态插入行。但问题是这些行是水平添加的。我希望每一排桌子都垂直堆放。到目前为止,我有: TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer); TableRow tablerow = new TableRow(this); tablerow.setOrientation(TableRow.VERTICAL); EditTe

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

    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


有人知道这里出了什么问题吗


谢谢

您最好有一个XML格式的表行项目,将其作为新视图进行膨胀,并相应地设置数据


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

尝试将第一行放入xml中,并旋转整个表格,必要时编辑文本视图。像这样

XML


我解决了这个问题,我有一个错误的变量名。但现在我有了一个新问题,我怎样才能在柱子上弄湿重物?因此,我想创建第二个列填充屏幕。您可以按如下方式编程设置权重新建TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1f)
            <TableLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/studentContainer" >
            </TableLayout >
<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));