编程设置保证金的TableRow保证金、TableRow

2023-09-13 00:41:33 作者:{校草}

我有 TableRows 在code动态创建的,我想设置页边距为这些 TableRows

I have TableRows created dynamically in the code and I want to set margins for these 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);

如何动态地设置页边距为这些?

How do I dynamically set the margins for these?

推荐答案

您必须设置的LayoutParams正常。保证金是布局的TableRow的属性,而不是,所以你必须设置所需的利润率在的LayoutParams。

You have to setup LayoutParams properly. Margin is a property of layout and not the TableRow , so you have to set the desired margins in the LayoutParams.

下面有一个样本code:

Heres a sample code:

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);