新しい行を作成し、手動でレイアウトを設定するテーブルビューがあります。各行には、画像、タイトルのテキストビュー、説明のテキストビューの 3 つの主要な要素があります。必要な場所に移動するために、2 つの線形レイアウトを使用します。説明用のテキストビュー(descViewという名前)にテキストを2行で表示したいのですが、代わりに-1つしか表示されません。何を間違えたのか、どのメソッドを呼び出すべきなのか、ビューにはテキストが 2 行で表示されます。テキストを(文字ではなく)単語ごとにラップする場合は完璧です。
私が今持っているもののスクリーンショット:
新しい行を作成するコード (「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;
}