我想在horizontalScrollView中的每个图像上显示图像和文本。
为此我在horizontalScrollView中使用RelativeLayout并动态添加ImageView和TextView。它如下所示。图像仅显示文本长度。
text1 text2 text3 text4 text5 text6 |
| | | | | | |
I1 | i2 | i3 | i4 | i5 | i6 | i7 |
| | | | | | |
以下是我的代码。
for (int j = 0; j < 7; j++){
imageView = new ImageView (con);
textView=new TextView(con);
imageView.setId(50+j);
textView.setId(50+j);
//set image to imageView
new vdoImageLoader(videos.get(j),imageView,con).execute(null, null);
if (j == 0) {
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageView.setLayoutParams(rlp1);
vdoRelativeLayout.addView(imageView);
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
textView.setLayoutParams(rlp2);
vdoRelativeLayout.addView(textView);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.RIGHT_OF, imageView.getId() - 1);
imageView.setLayoutParams(rlp2);
vdoRelativeLayout.addView(imageView);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rlp1.addRule(RelativeLayout.RIGHT_OF, imageView.getId()-1);
rlp1.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId()-1);
textView.setLayoutParams(rlp1);
vdoRelativeLayout.addView(textView);
}
}