1
我需要将GridView中的项目作为单独的对象进行重绘,例如在下载所代表的游戏时显示进度条。所以我为View创建了一个包装器。 问题是网格项目根本不显示。 为简单起见,我只在LibraryAlbumView
中添加了一张图片。
如果我setBackgroundColor(0xFF00FF00);
我在网格单元中看到这个绿色背景,但没有其他的东西。 在我的包装:Android GridView Adapter显示空白视图
public class LibraryAlbumView extends LinearLayout {
private Context context;
public LibraryAlbumView(Context context) {
super(context);
this.context = context;
ImageView image;
image = new ImageView(context);
image.setImageDrawable(context.getResources().getDrawable(
R.drawable.delete));
this.addView(image);
}
和我的适配器代码:
public class LibraryAlbumAdapter extends BaseAdapter {
private Context context;
public LibraryAlbumAdapter(Context context) {
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
gridItem = new LibraryAlbumView(context);
return gridItem;
} else {
return (LibraryAlbumView) convertView;
}
然而
,如果我的包装改为LibraryAlbumView extends ImageView
并在其构造我更改为:
super(context);
this.context = context;
setImageDrawable(context.getResources().getDrawable(
R.drawable.delete));
我看到那里的图像
谢谢,我没有包括getCount()只是为了简化问题,是的,它是在代码中,否则它不会编译。我曾尝试设置宽度和高度像这样\t \t this.addView(image,200,200); 但它没有帮助 –
你有没有试过用这样的LayoutParams:'addView(image,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT))'? – SDJMcHattie
当您从适配器中的getView()'返回它时,还要在LinearLayout本身上设置LayoutParams – SDJMcHattie