Android Location: Quadratic Views

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

  • The view should be quadratic, i.e. The height and width of the view must match.
  • The view should fill out the entire WIDTH.

It should look like this (the square should be vertical):

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

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

But: now I want to have an extra view that is aligned with the top line of the square view, and that is where my approach fails. If you now set the view width to FILL_PARENT and its height to WRAP_CONTENT, the image will no longer fit the entire width.

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

Thanks in advance!

+7
source share
3 answers

Look at the main answer to this question.

Android platform with square buttons

+4
source

If you want two views to be next to each other, none of them can have FILL_PARENT. It looks like you need a horizontal LinearLayout containing an ImageView and another view. Then set android:layout_weight="1" and android:layout_width="0dp" in the ImageView, which will cause ImageView to fill the parent element. Then set the layout_width of the other view to "wrap_content".

 <LinearLayout android:width="FILL_PARENT" android:height="WRAP_CONTENT"> <ImageView android:layout_height="WRAP_CONTENT" android:layout_width="0dp" android:layout_weight="1" /> <OtherView android:layout_height="WRAP_CONTENT" android:layout_width="WRAP_CONTENT" /> </LinearLayout> 
0
source

I created the LinearLayout extension that does exactly what you want. For convenience, I wrapped this application in the Android library. See https://github.com/ronaldsteen/Android-SquareLayout-Library

0
source

All Articles