Java 如何以编程方式设置列以在Android中使用dynamic?

Java 如何以编程方式设置列以在Android中使用dynamic?,java,android,row,android-tablelayout,Java,Android,Row,Android Tablelayout,我创建了一个如下表: TableLayout table = new TableLayout(getApplicationContext()); table.setColumnStretchable(1, true); table.setColumnStretchable(2, true); tableRow = new TableRow[20]; tableRow[i] = new TableRow(getApplicationContext()); tableRow[i].setGravi

我创建了一个如下表:

TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);

tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
for (int j = 0; j < 4; j++) {
    statistics = new TextView[4];
    statistics[i] = new TextView(getApplicationContext());
    statistics[i].setText("Text");
    statistics[i].setGravity(Gravity.LEFT);
    tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
TableLayout table=newtablelayout(getApplicationContext());
table.setColumnStretchable(1,true);
table.setColumnStretchable(2,true);
tableRow=新的tableRow[20];
tableRow[i]=新的tableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
对于(int j=0;j<4;j++){
统计数据=新文本视图[4];
统计信息[i]=新文本视图(getApplicationContext());
统计学[i].setText(“文本”);
统计学[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
该代码的结果是:

我想做到这一点:

这怎么可能

用这个

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*"

   <TableRow 
        android:layout_weight="1"
        android:gravity="center">

        <ImageButton
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:scaleType="center"
            android:src="@drawable/ic_1" 
            android:background="@null" />

        <ImageButton
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:scaleType="center"
            android:src="@drawable/ic_2" 
            android:background="@null"/>

        <ImageButton
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:scaleType="center"
            android:src="@drawable/ic_3" 
            android:background="@null" />
    </TableRow>
 </TableLayout>

谢谢你,但我需要它。我没有使用
XML
。我用代码版本更新了答案,是一样的,但在代码中,试试看。你还有其他想法吗?
        TableLayout table = new TableLayout(getApplicationContext());
        table.setColumnStretchable(1, true);
        table.setColumnStretchable(2, true);
        table.setStretchAllColumns(true);

        tableRow = new TableRow[20];
        tableRow[i] = new TableRow(getApplicationContext());
        tableRow[i].setGravity(Gravity.CENTER);
        for (int j = 0; j < 4; j++) {
            statistics = new TextView[4];
            statistics[i] = new TextView(getApplicationContext());
            statistics[i].setText("Text");
            statistics[i].setGravity(Gravity.LEFT);
            tableRow[i].addView(statistics[i]);
        }
        table.addView(tableRow[i]);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TableLayout table = new TableLayout(getApplicationContext());
        table.setStretchAllColumns(true);

        for (int i = 0; i<20; i++) {

            TableRow[] tableRow = new TableRow[20];
            tableRow[i] = new TableRow(getApplicationContext());
            tableRow[i].setGravity(Gravity.CENTER);

            TextView pos = new TextView(getApplicationContext());
            pos.setGravity(Gravity.LEFT);
            pos.setText(String.valueOf(i) + ". " + getName(i));

            TextView a = new TextView(getApplicationContext());
            a.setGravity(Gravity.LEFT);
            a.setText("2/9");

            TextView points = new TextView(getApplicationContext());
            points.setGravity(Gravity.LEFT);
            points.setText("2/9");

            tableRow[i].addView(pos);
            tableRow[i].addView(a);
            tableRow[i].addView(points);

            table.addView(tableRow[i]);
        }

        RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
        container.addView(table);
    }

    private String getName(int i) {

        if (i == 2) {
            return "Recooooooooooooooord";
        } else if (i == 3) {
            return "Recooooooord";
        }

        return "Fran";
    }