Search code examples
androidimageandroid-layoutscale

Android - scale image to available space


I'm familiar with Windows Phone XAML and its Grid with Row and Columns. Now I would like to develop an Android app with similar layout:

 ______________________
|                      |
|                      |
|  1' row - two images |
|                      |
|                      |
|                      |
|                      |
|----------------------|
|  2' row - TextView   |
|                      |
|----------------------|
| 3' row - two buttons |
|______________________|

It's easy to get this layout in XAML with GridRows. First row with Height="*". Second and last row with Height="Auto". How can I get this in Android? I've tried with TableLayout and it works fine on big screens. But on small screen I can only see first and second row without buttons. Buttons and text always should be visible and images should fit to the rest of available screen space. Images are transparent and loaded one on the other. Thanks for any tips.


Solution

  • Use a LinearLayout. For the first item, set its layout_height="0dp" and the layout_weight="1". For the other two, put layout_height="wrap_content". This will give the bottom 2 elements as much room as they need but no more, and divide the remaining amount between all 0dp heights based on weight ratio. Since only 1 item has a weight, it will all go there.