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

时间:2015-11-27 14:18:26

标签: 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].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]);

此代码的结果是:

enter image description here

我想实现这个目标:

enter image description here

这怎么可能?

1 个答案:

答案 0 :(得分:3)

有了这个

<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”和/或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