我有 tableview,在其中创建新行并手动设置它们的布局。在每一行中都有 3 个主要元素:图像、标题的 textview 和描述的 textview。要将它们移动到我想要的位置,我使用了两个线性布局。我想要用于描述的 textview(命名为 descView)以两行显示文本,但相反 - 它只显示一个。我做错了什么或调用什么方法,因此视图以两行显示文本。如果它按单词(而不是字符)包装文本将是完美的。
我现在拥有的截图:
创建新行的代码('New' 是我保存信息(文本、图像等)的对象):
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;
}