我有内部ImageViews一个GridView。我有3人各行。我可以正确设置WRAP_CONTENT和scaleType = CENTER_CROP的宽度,但我不知道如何设置ImageView的大小是一个正方形。这是我做了到现在为止,这似乎是确定的,除了高度,那就是静:
I have a GridView with ImageViews inside. I have 3 of them for each row. I can set correctly the width with WRAP_CONTENT and scaleType = CENTER_CROP, but I don't know how to set the ImageView's size to be a square. Here's what I did until now, it seems to be ok except the height, that is "static":
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300));
我的适配器内部做这件事。
I'm doing it inside an adapter.
最好的办法是继承的ImageView
自己,覆盖措施通:
The best option is to subclass ImageView
yourself, overriding the measure pass:
public class SquareImageView extends ImageView {
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
...
}