ImageView的与动态宽度方? [英] ImageView be a square with dynamic width?

查看:127
本文介绍了ImageView的与动态宽度方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部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);
  }

  ...

}

这篇关于ImageView的与动态宽度方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆