小编Jav*_*lar的帖子

Android:以编程方式在TableLayout中拉伸行

我正在尝试动态创建一个TableLayout,它的所有行都被拉伸,就像本教程中所示.我已经通过XML完成了它,但我想从Activity中完成它.这是我迄今为止尝试过的代码没有成功:

public View getTableWithAllRowsStretchedView() {
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setStretchAllColumns(true);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
    tableLayout.setWeightSum(4);


    for (int i = 0; i < 4; i++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setGravity(Gravity.CENTER);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));

        for (int j = 0; j < 4; j++) {
            Button button = new Button(this);
            final int buttonNumber = (j + i * 4);
            button.setText("" + buttonNumber);
            button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));

            tableRow.addView(button);
        }
        tableLayout.addView(tableRow);
    } …
Run Code Online (Sandbox Code Playgroud)

android android-layout

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

android-layout ×1