Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout: Quadratic views

In an Android application I need to add a view to my RelativeLayout that obeys the following rules:

  1. The view should be quadratic, i.e. the height and the width of the view must match.
  2. The view should fill the whole WIDTH.

It should look like this (the square should be centered vertically):

+-------------+
|  activity   |
|             |
|+-----------+|
||           ||
||  square   ||
||           ||
||           ||
|+-----------+|
|             |
|             |
+-------------+

In the end I want to have an ImageView that displays a quadratic image. Currently I did this by setting both width and height of the view to FILL_PARENT, and let the ImageView draw the image correctly (using scaleType=centerInside).

But: Now I want to have an additional view that is aligned with the top line of the square view, and this is where my approach fails. If I now set the width of the view to FILL_PARENT and the height of it to WRAP_CONTENT, the image will not fit the whole width any longer.

Do you have a solution to this problem? It would be preferrable if this could be done by just writing XML, if possible.

Thanks in advance!

like image 432
mreichelt Avatar asked Mar 16 '11 16:03

mreichelt


1 Answers

Look at the top answer on this question.

Android layout with square buttons

like image 147
Nick Avatar answered Oct 04 '22 02:10

Nick