我正在实现一个列出图像的GridView,类似于缩略图,下面是我在适配器中的getView方法:
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
MyData theData=datas.get(pos);
String src=theData.getSrc();
imageView.setTag(src);
Drawable dr = imgFetcher.loadImage(this, imageView);
imageView.setImageDrawable(dr);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85);
return imageView;
}
正如您可能注意到的, imageView.setLayoutParams(new GridView.LayoutParams(85,85)硬编码图像的宽度和高度,但我不想硬编码,相反,我想要使宽度填充父级和高度与宽度相同。任何人都知道如何实现它?
答案 0 :(得分:0)
如果您知道GridView中有多少列,并且您有GridView对象(我们称之为“gv”),您可以这样做:
imageView.setLayoutParams(new GridView.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, gv.getWidth()/columnNumber));