gpt4 book ai didi

android - 以编程方式为 TableRow 设置边距

转载 作者:IT老高 更新时间:2023-10-28 23:16:41 26 4
gpt4 key购买 nike

我在代码中动态创建了 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com