我已经动态创建了一个填充了不同长度的键和值的表。在TableLayout中动态调整TextView宽度
当我尝试设置宽度来包装内容时,宽度是相对于各个键大小设置的,而不是整个表的宽度。
这会导致第二个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;
}
结果
不知道这会为宽度连续工作TextView的已设置。明天再试,让你知道。 – PassKit
可疑 - TextView的大小已经被确定,相对于该行中其他TextView的宽度。因此这个班级没有效果。 – PassKit