本文介绍了ImageView 是动态宽度的正方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 GridView,里面有 ImageViews.我每行有 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
,覆盖measure pass:
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 是动态宽度的正方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!