这是一个棘手的问题,我将margin设置为表格布局的行中的textview。 看到下面:
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.setBackgroundResource(R.color.rowColor);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(4, 4, 4, 4);
TextView tv = new TextView(this);
tv.setBackgroundResource(R.color.textviewColor);
tv.setText("hello");
tr.addView(tv, params);
TextView tv2 = new TextView(this);
tv2.setBackgroundResource(R.color.textviewColor);
tv2.setText("hi");
tr.addView(tv2, params);
tl.addView(tr);
setContentView(tl);
导入了的LayoutParams在表行使用所需的类是:
import android.widget.**TableRow**.LayoutParams;
重要的是要注意,我增加了对表行的类。同样可以使用许多其他类来使用LayoutParams,例如:
import android.widget.**RelativeLayout**.LayoutParams;
import android.widget。 LinearLayout .LayoutParams;
所以相应地使用。
固体。谢谢... – trgraglia
这个调用需要API级别为19. –
@SanjayKumar - 不,没有限制,它需要API级别19 – Debarati