如何在TableRow和android之间设置边距

如何在TableRow和android之间设置边距,android,Android,我找到了很多关于TableRow边距的解决方案,但当我尝试使用这些行时,它们根本没有边距 这是我的代码: TableLayout table = (TableLayout)findViewById(R.id.myTableLayout); for (int i = 0; i < 3; i++) { TableRow tr = new TableRow(this); LayoutParams layoutPar

我找到了很多关于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));
        }
TableLayout table=(TableLayout)findviewbyd(R.id.myTableLayout);
对于(int i=0;i<3;i++){
TableRow tr=新的TableRow(本);
LayoutParams LayoutParams=新的LayoutParams(
LayoutParams.FILL\u父级,LayoutParams.FILL\u父级);
int leftMargin=110;
int topMargin=100;
int rightMargin=100;
int-bottomMargin=100;
layoutParams.setMargins(左边距、上边距、右边距、下边距);
tr.setLayoutParams(layoutParams);
tr.setBackgroundResource(R.drawable.shelf_bar);
table.addView(tr,新的TableLayout.LayoutParams(
LayoutParams.FILL\u父级,
LayoutParams.FILL_PARENT));
}
这是我的预期结果:

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

用这个可以吗

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);
这是有效的:

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

我的代码目前非常小,在XML文件中只有一个简单的表,我的问题中的代码都是java代码对不起。。。我的意思是,你能不能将边距设置为102102,而不是110100100……。它仍然不动,这真的让我发疯。你能再做一件事吗?试试这行tr.setBackgroundResource(R.drawable.shelf_bar);