tags:

views:

39

answers:

1

I am trying to draw some boxes in my activity, which will contain other layout elements, in XML. I want to do this using the relative dimensions, e.g. specify half of the height of the screen for each box. I understand that the correct way to do this is to set the initial height to 0px and use layout_weight, but I can't get the following example to work. I am expecting it to draw two identical rectangles (defined in /drawable/rect) in different vertical halves of the screen, but the screen is blank:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
 <ImageView
android:src="@drawable/rect"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
  android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"/>
 <ImageView
android:src="@drawable/rect"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
  android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="0px"
android:layout_weight="1"/>
</RelativeLayout>

Does anyone have any ideas?

+1  A: 

Why is it?

android:layout_height="0px"

That makes the rectangles to have zero height, which might explain why they don't show up.

iniju
That's the way to do it according to [Mark Murphy](http://is.gd/1Y2DM)
Stev_k
Hmmm, maybe it doesn't workj because that advice is for LinearLayout, but you use RelativeLayout. Two suggestions: 1) To use wrap_content for both OR 2) To use wrap content for the first and fill_parent for the second.
iniju
I mean for layout_height
iniju
Ok, thanks, but doesn't that mean that I have to define the size of the rectangle before creating the layout rather than defining it as a % of the screen?
Stev_k
additional sidenote: if the layout_width of the images wouldn't be fill_parent, the elements also should have an id and then s.th. like android:layout_below="@id/image_view_1" set for the second image view. But in your case, iniju is right regarding the height attribute.
Mathias Lin
OK, I sorted it out - you were completely right. You need a Linear Layout to split a screen like that. Solution was to use a Linear Layout, with nested Relative Layouts. Thanks for all assistance
Stev_k