Stev_k
Stev_k

Reputation: 2126

Can't get android:layout_weight to work

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?

Upvotes: 0

Views: 838

Answers (1)

iniju
iniju

Reputation: 1094

Why is it?

android:layout_height="0px"

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

Upvotes: 1

Related Questions