Программно заданный запас для TableRow
У меня TableRows
динамически создается в коде, и я хочу установить поля для этих TableRows
.
Мой TableRows
создан следующим образом:
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btnManageGroupsSubscriptions = new Button(this);
btnManageGroupsSubscriptions.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 40));
tr.addView(btnManageGroupsSubscriptions);
contactsManagementTable.addView(tr);
Как мне динамически установить для них поля?
Ответы
Ответ 1
Вы должны правильно настроить LayoutParams. Маржа - свойство макета, а не TableRow, поэтому вам нужно установить желаемые поля в LayoutParams.
Вот пример кода:
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
Ответ 2
Это работает:
TableRow tr = new TableRow(...);
TableLayout.LayoutParams lp =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10,10,10,10);
tr.setLayoutParams(lp);
------
// the key is here!
yourTableLayoutInstance.addView(tr, lp);
Вам нужно снова добавить TableRow в TableLayout, передав параметры макета!