【发布时间】:2012-02-23 04:01:40
【问题描述】:
我有这些动态生成的文本字段。但我似乎无法为它们设置布局参数。请告诉我我做错了什么。
我能够生成没有布局参数的字段。但是,如果我使用LayoutParams,它甚至不会生成。
代码:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(trparams);
cg[i] = new EditText(this);
weight[i] = new EditText(this);
cg[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
weight[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
//Here's where it goes wrong. By adding fieldparams, the text field doesn't even get generated.
LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(10, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tr.addView(cg[i], fieldparams);
tr.addView(weight[i], fieldparams);
table.addView(tr);
【问题讨论】:
-
你在表格上使用 linearlayout.layoutparams 吗?它应该是 tablerow.layoutparams
-
不错的评论...但稍作修正 Amit.it 将是 TableLayout 而不是 TableRow
标签: android layout dynamic parameters android-edittext