我想有一个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()));

09-30 15:15
查看更多