views:

566

answers:

4

I've got a ListView with items composed of RelativeLayouts. This is the relevant XML from the list items:

<?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="wrap_content">

    <TextView
        android:id="@+id/xx"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true" 
        android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/xx" />

    <TextView
        android:id="@+id/tag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/xx"
        android:layout_below="@id/title" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tag"
        android:layout_below="@id/title" />

</RelativeLayout>

On Android 2.1 (tested on a Nexus One), this shows the desired behavior: Android 1.5

On Android 1.5 however (tested on a HTC Hero), it shows up like this: Android 1.5

[edit] On 1.6 (emulator), it works as expected as well.

The small grey line on the top left is what shows up in the first pic as "xx", so that should be vertically centered. As far as I can see, the XML dictates this, but for some reason, 1.5 ignores it.

Why is this? I can't find anything about this difference, and I've been brute forcing any combination of layout_center, center, alignParent*, but to no avail...

Can anyone shed some light on this? Thanks!

A: 

For one, judging by the fact that it's broken in the earliest version you're testing with and works as expected in the later versions...sounds like a bug that was fixed.

However, unless I'm oversimplifying because you're only showing a few basic sample screen shots, I would do this with nested LinearLayouts anyway.

Rich
A single RelativeLayout is much better than nested LinearLayouts.
Romain Guy
In programming, I rarely use the term "better than" without context. All programming decisions are relative to a lot of variables. If performance differences are negligible, then there are trade offs between things like code readability, flexibility, ease of use, etc. A single RelativeLayout may be less code and thus more readable in some cases, but nested LinearLayouts are much easier to make small modifications to such as inserting new elements, etc w/o affecting other elements. Also, it could very well fix this guy's bug in 1.5...making LinearLayouts immediately better for his case.
Rich
One Relative Layout is better memory-wise and performance-wise over multiple nested Linear Layouts.
MasterGaurav
I'm actually coming from a working view with nested LinearLayouts. :) But on the Hero, I got a StackOverFlowError, so I optimized a lot of layouts. Maybe for this one I should go back to the original, with LinearLayouts.
benvd
+1  A: 

For relative layout layout_gravity is not used.

Also, you're using conflicting attribtues centerInParent and alignParentLeft.

Use only one of them.

You can use layout_centerVertical="true" layout_alignParentLeft="true"

MasterGaurav
Yeah, those were leftovers from my attribute-brute forcing. :) Correcting this doesn't seem to help. Thanks anyway.
benvd
A: 

RelativeLayout receives many bug fixes in 1.6 and then 2.0 :)

Romain Guy
Sadly and as far as I know, in Belgium most devices still run 1.5, and the app I'm writing is mainly directed at Belgian users. :(
benvd
A: 

I just had the same problem! For me the issue is that a top-levl RelativeLayout with a layout_height or layout_width of "wrap_content"does strange and counter-intuitive placement.

The code I fought with was answering this layout question.

Charles Merriam