【发布时间】:2018-04-04 03:41:33
【问题描述】:
我想在 tablerow 中创建多行 textview 并使用以下代码,但 column 的长文本覆盖了整个宽度。如何在表格中设置多行文本视图?
TableLayout getClassBookTable(ArrayList<String> titles, String json) {
TableLayout tableLayout = new TableLayout(this);
try {
tableLayout.addView(getTitles(titles, false));
JSONArray jsonArray = new JSONArray(json);
// TableRow.LayoutParams params = new TableRow.LayoutParams(
// TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);
// params.span = 1;
for (int i = 0; i <jsonArray.length(); i++) {
TableRow tableRow = new TableRow(this);
JSONObject jsonObject = jsonArray.getJSONObject(i);
TextView snTextView = getTableTextView();
snTextView.setText(jsonObject.getString("s.n."));
// snTextView.setLayoutParams(params);
tableRow.addView(snTextView);
TextView bookTextView = getTableTextView();
bookTextView.setText(jsonObject.getString("book"));
// bookTextView.setLayoutParams(params);
tableRow.addView(bookTextView);
TextView subjectTextView = getTableTextView();
subjectTextView.setSingleLine(false);
subjectTextView.setText(jsonObject.getString("subjectString"));
// subjectTextView.setLayoutParams(params);
tableRow.addView(subjectTextView);
TextView publisherTextView = getTableTextView();
publisherTextView.setSingleLine(false);
publisherTextView.setText(jsonObject.getString("publisher"));
// publisherTextView.setLayoutParams(params);
tableRow.addView(publisherTextView);
TextView authorTextView = getTableTextView();
authorTextView.setText(jsonObject.getString("author"));
// authorTextView.setLayoutParams(params);
tableRow.addView(authorTextView);
tableLayout.addView(tableRow);
}
已编辑 当我添加以下代码时 tableLayout.setShrinkAllColumns(true); tableLayout.setColumnStretchable(0, true); 其布局如下图所示
【问题讨论】:
-
您的预期输出是什么以及您从上面的代码中得到什么输出?您需要提供有关您的问题的更多信息
-
分享你的xml代码
-
抱歉没有xml文件只有代码
-
@nilesh 我已经添加了它的截图