Android layout: Quadratic views
In an Android application I need to add a view to my RelativeLayout that obeys the following rules:
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!
Look at the top answer on this question.
Android layout with square buttons
If you want two views to be beside each other then neither one can have FILL_PARENT. Sounds like what you need is a horizontal LinearLayout containing the ImageView and the other view. Then set android:layout_weight="1"
and android:layout_width="0dp"
on the ImageView, that will make the ImageView fill the parent. 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>
I've created an extension of LinearLayout that does exactly what you want. I've wrapped the thing in an Android Library project for convenience. See https://github.com/ronaldsteen/Android-SquareLayout-Library
链接地址: http://www.djcxy.com/p/14830.html上一篇: 以编程方式将图像添加到RelativeLayout
下一篇: Android布局:二次视图