2

我找到了很多关于 TableRow 边距的解决方案,但是当我尝试行时根本没有边距。

这是我的代码:

        TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);

        for (int i = 0; i < 3; i++) {
            TableRow tr = new TableRow(this);
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            int leftMargin=110;
            int topMargin=100;
            int rightMargin=100;
            int bottomMargin=100;
            layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
            tr.setLayoutParams(layoutParams);
            tr.setBackgroundResource(R.drawable.shelf_bar);

            table.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
        }

这是我的预期结果:

在此处输入图像描述

请任何人指出我的错误。谢谢

4

2 回答 2

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 再次传递布局参数!

于 2014-06-20T15:11:04.827 回答
0

用这个可能

TableRow tr = new TableRow(this);  
TableLayout.LayoutParams tableRowParams=
  new TableLayout.LayoutParams
  (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT);

        int leftMargin=10;
        int topMargin=2;
        int rightMargin=10;
        int bottomMargin=2;

tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

tr.setLayoutParams(tableRowParams);
于 2012-06-23T09:38:23.027 回答