Динамическая компоновка setLayoutParams не устанавливается - PullRequest
0 голосов
/ 07 июля 2011

У меня есть подпрограмма, которая динамически создает TableRow s внутри TableLayout, накачанного из XML.Я пытаюсь установить TableRow s для определенной ширины, заполняя их пользовательской кнопкой (ButtonElement) ... но ничего не появляется.Выйдя из ширины TableRow, кажется, что она установлена ​​в 0, что может объяснить, почему она не отображается.

final Drawable normalBtnBg = getResources().getDrawable(R.drawable.button_states);
TableRow tableRow;
for (int x = 0; x < xDim; x++) {
    tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableLayout.LayoutParams(400, 20));
    for (int y = 0; y < yDim; y++) {
        // create the new button
        mButtons[x][y] = new ButtonElement(this, x, y);
        mButtons[x][y].setBackgroundDrawable(normalBtnBg);
        mButtons[x][y].setLayoutParams(new LayoutParams(20, 20));

        // add the button to the TableLayout
        tableRow.addView(mButtons[x][y]);
    }

    // add the TableRow to the table
    mButtonGrid.addView(tableRow, new TableLayout.LayoutParams(400, 20));
    Log.d(LOG_TAG, "trWidth - " + tableRow.getWidth());
}

tableRow.getWidth() всегда возвращает значение 0 ... Iпроверил, что кнопки генерируются, как будто я добавляю их непосредственно в mButtonGrid, они отображаются (конечно, не в TableRow, просто вертикально вниз).

Любая помощь приветствуется.

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

понял это, см мой ответ ниже.

1 Ответ

2 голосов
/ 07 июля 2011

FYI, решил эту проблему ... Я использовал TableLayout.LayoutParams, но мне нужно было использовать TableRow.LayoutParams, чтобы заставить его работать (от здесь ):

final Drawable normalBtnBg = getResources().getDrawable(R.drawable.button_states);
TableRow tableRow;
for (int x = 0; x < xDim; x++) {
    tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableRow.LayoutParams(400, 20));
    for (int y = 0; y < yDim; y++) {
        // create the new button
        mButtons[x][y] = new ButtonElement(this, x, y);
        mButtons[x][y].setBackgroundDrawable(normalBtnBg);
        mButtons[x][y].setLayoutParams(new TableRow.LayoutParams(20, 20));

        // add the button to the TableLayout
        tableRow.addView(mButtons[x][y]);
    }

    // add the TableRow to the table
    mButtonGrid.addView(tableRow, new TableLayout.LayoutParams(400, 20));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...