我有以下代码,试图设置保证金:
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView name = new TextView(this);
name.setText(Html.fromHtml(venues.get(j).name + "<br><br>" + venues.get(j).getFullAddress()));
然而,这并未设定保证金。我在这里做错了什么?
答案 0 :(得分:1)
编辑。哎呀,我的坏,TableLayout.LayoutParams很好。在TableRow的子项上使用TableRow.LayoutParams。
我刚刚运行了这段代码(大部分是您的代码)并且边距设置正确
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=100;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView tv = new TextView(this);
tv.setText("EXAMPLE ROW");
TableRow.LayoutParams textParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textParams);
tr.addView(tv);
tlayout.addView(tr);
答案 1 :(得分:-1)
不是这样吗?
TableRow tr = new TableRow(Activity.this)
我认为你也需要一张桌子。
为什么不选择XML?
<TableRow
android:id="@+id/tr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>