我在代码中动态创建了 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);
如何动态设置这些边距?
最佳答案
您必须正确设置 LayoutParams。 Margin 是布局的属性,而不是 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);
关于android - 以编程方式为 TableRow 设置边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4577644/