TextView不会展开以匹配TableRow高度

时间:2013-02-21 07:29:07

标签: android android-linearlayout

如何拉伸TextView使其填满整个TableRow高度?这是我现在所获得的截图,

我希望TextView展开TableRow

以下是我创建行的方法:

row = new TableRow(this);

desc = new TextView(this);

desc.setBackgroundResource(R.drawable.borders);
desc.setTypeface(Typeface.SERIF, Typeface.BOLD);  
desc.setText("Production Date");
desc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

String date = gendataObj.getString("date");
text = new TextView(this);
text.setBackgroundResource(R.drawable.borders);
text.setText(date);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

row.addView(desc);
row.addView(text);

table.addView(row, new TableLayout.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

1 个答案:

答案 0 :(得分:4)

变化:

text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

为:

text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));

除非另有说明,否则您不指定高度(只是宽度),默认情况下它将保持为WRAP_CONTENT,这会导致TextView与内容(文本)一样高。