[英]Gallery with image and text
我正在尝试建立一个图片下方带有文字的画廊,但是尽管我一直关注这里的所有回应,但仍未实现我的目标。
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
TextView tv = new TextView(ll.getContext());
tv.setTag(mText[position]);
tv.setText(mText[position]);
tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
ll.addView(tv);
// The preferred Gallery item background
//i.setBackgroundResource(mGalleryItemBackground);
return ll;
//return i;
}
我不知道为什么(也许这是最愚蠢的事情),但是我的图像没有出现:)
我相信您忘记将图像视图添加到线性布局中,只需添加:
ll.addView(i);
另外,除非您的图像很少且网格滚动很少甚至没有,否则您不会回收可能导致问题的convertView。
您应该检查convertView是否为null,以及是否不简单地更改现有convertView的文本和图像。
这是自定义网格视图的一个很好的示例: http : //vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.