如何以编程方式设置列以在 Android 中具有动态?

How to set columns programmatically to have dynamic with in Android?

我创建了一个 table 这样的:

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
    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>

三个按钮对齐。我认为您的代码中需要 gravity="center"and/or android:stretchColumns="*" 。

更新

试试这个:

        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";
    }

结果如你所愿,可以看到结果here