12

テーブルレイアウトに列と行を動的に追加する方法を理解しようとしています。

この簡単な例があります。ただし、実行時に最初の列のみが表示されます。

1 つではなく 4 つの列を表示するために何が欠けているか教えてもらえますか?

package com.apollo.testtablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestTableLayoutActivity extends Activity {
    /** Called when the activity is first created. */

    String col1;
    String col2;
    String col3;
    String col4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);

        for (int x = 1; x < 10; x++) {

            col1 = "(" + x + ") Column 1";
            col2 = "(" + x + ") Column 2";
            col3 = "(" + x + ") Column 3";
            col4 = "(" + x + ") Column 4";

            TableRow newRow = new TableRow(this);

            TextView column1 = new TextView(this);
            TextView column2 = new TextView(this);
            TextView column3 = new TextView(this);
            TextView column4 = new TextView(this);

            column1.setText(col1);

            newRow.addView(column1);
            newRow.addView(column2);
            newRow.addView(column3);
            newRow.addView(column4);

            tl.addView(newRow, new TableLayout.LayoutParams());
        }
    }

}
4

2 に答える 2

6

You need to have the setText for each of the columns text

column1.setText(col1); 
column2.setText(col2); 
column3.setText(col3); 
column4.setText(col4); 
于 2012-10-12T22:10:21.717 に答える
0

各列には new TableLayout.LayoutParams() も必要であることを忘れないでください。これは、より複雑なビューをテーブル レイアウトに追加しようとしたときに犯した間違いです。

newRow.addView(view, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                            TableRow.LayoutParams.WRAP_CONTENT) );
于 2014-10-07T12:22:43.973 に答える