0

我是android应用程序开发的初学者。请帮助我如何在表中创建多行以及创建行的关键性能。我现在可以创建 3 行。但我想在表格中输入数据时自动生成行。

4

1 回答 1

0

尝试这个:

    //Declare mytable in layout XML file.
    TableLayout tl = (TableLayout) findViewById(R.id.mytable);

    //loop through number of times u want to create row.
    for (int current = 0; current < numofrowsrequired; current++)
    {
        // Create a TableRow and give it an ID
        TableRow tr = new TableRow(this);
        tr.setId(100+current);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));   

        //add required views with required parameters
        TextView labelTV = new TextView(this);
        labelTV.setId(200+current);
        labelTV.setText(provinces[current]);
        labelTV.setTextColor(Color.BLACK);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);
        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
于 2013-02-06T04:44:29.130 回答