gpt4 book ai didi

android - ImageView 是动态宽度的正方形?

转载 作者:IT老高 更新时间:2023-10-28 13:14:11 29 4
gpt4 key购买 nike

我有一个 GridView,里面有 ImageViews。我每行有 3 个。我可以使用 WRAP_CONTENT 和 scaleType = CENTER_CROP 正确设置宽度,但我不知道如何将 ImageView 的大小设置为正方形。这是我到目前为止所做的,除了高度之外似乎还可以,即“静态”:

imageView = new ImageView(context);     
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300));

我是在适配器内部进行的。

最佳答案

最好的选择是自己继承 ImageView,覆盖测量 channel :

public class SquareImageView  extends ImageView {

...

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}

...

}

关于android - ImageView 是动态宽度的正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16506275/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com