我有一个表格视图(tableview),在其中手动创建新的行并设置它们的布局。每一行都有三个主要元素: 图像、用于标题的文本视图和用于描述的文本视图。为了将它们移动到我想要的位置,我使用了两个线性布局。我希望描述文本视图(descView)显示两行文字,但实际上只显示了一行。我做错了什么或者应该调用哪个方法,使视图显示两行文字。如果能按单词换行 (而不是字符),那就太棒了。
现在我有一个屏幕截图:
这是创建新行的代码('New'是我保存信息(文本、图像等)的对象)。
现在我有一个屏幕截图:
![enter image description here](https://istack.dev59.com/asoBZ.webp)
private TableRow formRow(New item) {
//prepare table row parameters
TableRow tr = new TableRow(this);
tr.setBackgroundResource(R.drawable.results_table_row);
tr.setVerticalGravity(Gravity.CENTER);
tr.setTag(item.getTitle());
tr.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
v.setBackgroundResource(R.drawable.results_table_row);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.results_table_row_touched);
}
return true;
}
});
//create main layout for content
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
//add image
int imageID = Integer.parseInt(item.getPictureSrc());
ImageView image = new ImageView(this);
image.setImageResource(imageID);
image.setPadding(5, 5, 5, 5);
layout.addView(image);
//add another layout for title and description
LinearLayout descLayout = new LinearLayout(this);
descLayout.setOrientation(LinearLayout.VERTICAL);
descLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//add title
TextView titleView = new TextView(this);
titleView.setText(item.getTitle());
titleView.setPadding(5, 0, 0, 0);
titleView.setTextAppearance(this, R.style.TableRowStyle_ChildTitle);
titleView.setLines(1);
descLayout.addView(titleView);
//add description
TextView descView = new TextView(this);
descView.setText(item.getDescription());
descView.setPadding(5, 0, 0, 0);
descView.setTextAppearance(this, R.style.TableRowStyle_ChildText);
descView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
descView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//descView.setEllipsize(TextUtils.TruncateAt.END);
descView.setLines(2);
descLayout.addView(descView);
//add description layout to main layout
layout.addView(descLayout);
//finally, add layout to row
tr.addView(layout);
return tr;
}
descView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));
,但这次它甚至没有显示任何文本。 - Paulius Vindzigelskisint widthForText = GlobalPrefs.getScreenWidth() - MAX_IMAGE_WIDTH - 10;
来计算文本宽度(屏幕宽度 - 图像宽度 - 边距和其他损失)。现在在你的帮助下它可以正常工作。 - Paulius Vindzigelskis