如何在 table 行中以编程方式设置 textview 和 edittext 参数?
how to set textview and edittext params programmatically in a table row?
我想通过代码添加 textview 和 edittext 参数。 这是我的代码
private void draw_table() {
// TODO Auto-generated method stub
TableLayout ll = (TableLayout) findViewById(R.id.input_table_2);
for (int i = 0; i < 2; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// setting textVIEW
textview = new TextView(this);
textview.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//textview.setPadding(0, 0, 5, 10);
//textview.setTextAppearance(this,
// android.R.style.TextAppearance_Medium);
textview.setText("Hello");
// setting editText
edittext = new EditText(this);
edittext.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
//edittext.setEms(10);
//edittext.setHint("gpa");
//edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(textview);
row.addView(edittext);
ll.addView(row, i);
}
}
但屏幕上没有显示任何内容。这段代码有什么问题?提前致谢:)
private void draw_table() { // get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.input_table_2);
for (int i = 0; i < 2; i++) {
// create a new TableRow
TableRow row = new TableRow(this);
// create a new TextView
TextView textview = new TextView(this);
// set the text to "text Hello"
textview.setText("Hello");
// create a EditText
EditText edittext = new EditText(this);
row.addView(textview);
row.addView(edittext);
// add the TableRow to the TableLayout
table.addView(row,i,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
使用TableRow.LayoutParams 设置您的小部件 LayoutParams。像这样
textview.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
另见how add and delete rows to table layout in java programically