开发者

Make ImageView be as tall as it is wide?

开发者 https://www.devze.com 2023-01-31 07:09 出处:网络
Is there a way to开发者_JAVA技巧 tell an ImageView to be as tall as it is wide? Something like: <ImageView

Is there a way to开发者_JAVA技巧 tell an ImageView to be as tall as it is wide? Something like:

<ImageView
  layout_width="fill_parent"
  layout_height="whatever width ends up being"
  />

Thanks


I've recently been trying to do something similar; to my knowledge there's no way to do this through XML. One thing you could do is set the height to 0 initially, and then upon layout, in code, check the width and set the height. Something like this:

final ImageView imageView = (ImageView)findViewById(R.id.my_image_view);
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int width = imageView.getWidth();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
        imageView.setLayoutParams(params);
    }
}

Now, not having tested this, if the rest of your layout is dependent upon the size of this view, I'm not certain what effect this code will have, and if the rest of the layout will adjust itself accordingly. Might get you started though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号