gpt4 book ai didi

android - 使布局宽度等于高度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:58 26 4
gpt4 key购买 nike

我想要一个 width=fill_parent 的 ImageView,并且高度应该与宽度相同。我不确定是否有办法在 xml 中指定它,这是创建我自己的 ImageView 派生类的唯一选择吗?:

public class MyImageView extends ImageView {

// Just return whatever the current width is?
private int measureHeight(int measureSpec) {
return getWidth();
}
}

这是要走的路吗,还有其他选择吗? (我不确定以上是否正确,例如不确定宽度测量是否在高度测量之前进行)

谢谢

最佳答案

可以通过方法获取宽度

imageView.getMeasuredWidth();

所以,你可以设置它的高度

imageView.setLayoutParams(new LayoutParams(imageView.getMeasuredWidth(), imageView.getMeasuredWidth()));

关于android - 使布局宽度等于高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263134/

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