ImageView - have height match width?

后端 未结 14 2498
长发绾君心
长发绾君心 2020-12-02 05:17

I have an imageview. I want its width to be fill_parent. I want its height to be whatever the width ends up being. For example:



        
14条回答
  •  青春惊慌失措
    2020-12-02 06:08

    You can't do it with the layout alone, I've tried. I ended up writing a very simple class to handle it, you can check it out on github. SquareImage.java Its part of a larger project but nothing a little copy and paste can't fix (licensed under Apache 2.0)

    Essentially you just need to set the height/width equal to the other dimension (depending on which way you want to scale it)

    Note: You can make it square without a custom class using the scaleType attribute but the view's bounds extend beyond the visible image, which makes it an issue if you are placing other views near it.

提交回复
热议问题