【发布时间】:2016-05-31 16:21:49
【问题描述】:
我已经动态创建了一个表,其中填充了不同长度的键和值。
当我尝试设置宽度以换行内容时,宽度是相对于单个键大小而不是整个表设置的。
这会导致第二个 TextView 太宽,一旦将具有较大宽度的键添加到表中。
我尝试在表格完成后重绘表格,但保留了原始的 TextView 宽度。
是否有一个我忽略的简单修复,或者我需要以编程方式获取最长的键宽度,然后手动设置第二个 TextView 的最大宽度?
layout.xml
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none">
<TableLayout
android:id="@+id/passData"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip">
</TableLayout>
</ScrollView>
activity.java
private TableRow generateRow(String key, String value, boolean isOdd) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (isOdd) {
tr.setBackgroundColor(getResources().getColor(R.color.colorTableRow));
}
TextView keyView = new TextView(this);
TextView valueView = new TextView(this);
keyView.setTypeface(null, Typeface.BOLD);
keyView.setPadding(30, 20, 30, 20);
keyView.setTextSize(15);
keyView.setText(firstCaps(key));
valueView.setText(value.replace(" ", "\u00A0"));
valueView.setSingleLine(false);
valueView.setMaxLines(20);
valueView.setPadding(0, 20, 30, 20);
valueView.setGravity(Gravity.CLIP_HORIZONTAL);
tr.addView(keyView);
tr.addView(valueView,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
return tr;
}
结果
【问题讨论】:
标签: android textview android-tablelayout