Android: программно растягивать строки в TableLayout - PullRequest
4 голосов
/ 13 марта 2012

Я пытаюсь создать 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);
    }

    linearLayout.addView(tableLayout);
    return linearLayout;
}

Большое спасибо заранее.

РЕДАКТИРОВАТЬ

Screenshot of what I have

enter image description here

what I expect

enter image description here

Ответы [ 2 ]

14 голосов
/ 13 марта 2012

Ваши строки нуждаются в родительском LayoutParams:

tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));
0 голосов
/ 29 декабря 2015

Luksprog ответ правильный.

для Растянуть в горизонтальном положении, сделайте это:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));

для Растянуть по вертикали, сделайте это:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));

для растяжки по ширине и высоте, выполните следующее:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...