Thanks for all your replies.
But as I said, it was only a sample and not the real layout.
So maybe you can help me with the real layout ?

I have a list.
Each list item have the following layout that is
- one picture on the left, centered vertically
- one picture on the right, centered vertically
- 3 lines of text on the middle, aligned between the edges of the 2
pictures.

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

                <ImageButton
                        android:id="@+id/img1"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"

                        android:src="@drawable/icon"
                    android:scaleType="center"
                    android:background="@null"

                        android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                />

                <ImageButton
                        android:id="@+id/img2"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"

                        android:src="@drawable/icon"
                    android:scaleType="center"
                    android:background="@null"

                        android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                />


                <TextView
                        android:id="@+id/text1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="textligne1"

                    android:layout_alignParentTop="true"
                    android:layout_toRightOf="@id/img1"
                    android:layout_toLeftOf="@id/img2"
                />

                <TextView
                        android:id="@+id/text2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="textligne2"

                    android:layout_below="@id/text1"
                    android:layout_alignLeft="@id/text1"
                    android:layout_alignRight="@id/text1"
                />

                <TextView
                        android:id="@+id/text3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="textligne3"

                    android:layout_below="@id/text2"
                    android:layout_alignLeft="@id/text1"
                    android:layout_alignRight="@id/text1"
                />

</RelativeLayout>

When I use this layout alone in an activity, it works (despite it use
all the screen) correctly.
In particular, pictures are centered vertically.

Now, I wan't to use this layout in a listview whose layout is just :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
>
        <ListView
                android:id="@+id/list"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
        />
</LinearLayout>

This time, pictures are not centered vertically in each row : there
are aligned at the top of each row.

Why does RelativeLayout render differently when in a ListView and when
used as root view of an Activity ?

I suppose I could render this with 2 LinearLayout, but as it is used
in a list (so, frequently) I just follow Romain's advice to replace
those 2 LinearLayout by 1 RelativeLayout.
In fact I now use RelativeLayout everywhere. I found that almost all
might be done.
I just
- still find strange (sorry Romain, but I'm not convinced ...) the
behavior with my original post
- think that sometimes, using a two pass analyze of the layout would
be very helpful to be able to use id declared later in the layout.
Because actually, we sometimes have to make layout that are not
logical with the rendering. For example, for a picture at the bottom
with a text line above, I would have to declare first the picture
aligned with the bottom of the parent, then declare the textview above
the picture instead of declaring them in the order they appear ...

Anyway, thanks again for your answers and any further help.

Note : If anybody have time to, here is the corresponding eclipse
sample project :
http://www.baroukh.com/layouttest.tgz

Mike

On May 3, 6:06 pm, Romain Guy <romain...@android.com> wrote:
> It's unfortunately a "working as expected," although I'd agree that
> the "expected" is surprising. If you read your layout you will realize
> it does not make much sense:
>
> - The container is as tall as its child
> - The child is as tall as its content
> - You tell the child to center its content (which is meaningless since
> both are the same height)
> - In the case ofRelativeLayoutyou tell the child to be aligned with
> the parent's bottom, which is meaningless since the container and the
> child have the same height already
>
> What happens is when RL sees "align child to bottom," it tries to do
> exactly that and grows to take as much space as available, and then
> aligns your child to the bottom.
>
> Your two layouts are definitely NOT supposed to be equivalent.
>
>
>
> On Mon, May 3, 2010 at 5:57 AM, mbaroukh <mike.baro...@gmail.com> wrote:
> > Hi all.
>
> > I suppose it's not abugbut a "works as designed" but I found this to
> > be really frustrating.
> > The use case is simplified just to the point that is a problem for me.
>
> > Here is the first layout that works :
> > <?xml version="1.0" encoding="utf-8"?>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> >    android:layout_width="fill_parent"
> >    android:layout_height="wrap_content"
> >    android:orientation="vertical"
>
> >                <TextView
> >                        android:layout_width="wrap_content"
> >                        android:layout_height="wrap_content"
> >                        android:text="Test"
> >                        android:gravity="center_vertical"
> >                />
> > </LinearLayout>
>
> > and the layout that doesn't works :
> > <?xml version="1.0" encoding="utf-8"?>
> > <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/
> > android"
> >    android:layout_width="fill_parent"
> >    android:layout_height="wrap_content"
>
> >                <TextView
> >                        android:layout_width="wrap_content"
> >                        android:layout_height="wrap_content"
> >                        android:text="Test"
> >                        android:gravity="center_vertical"
> >                        android:layout_alignParentBottom="true"
> >                />
> > </RelativeLayout>
>
> > In the first case, "Test" is written to the top of the screen.
> > In the second case, "Test" is written in the miiddle of the screen.
>
> > It seems, for me, that LinearLayout perfoems correctly.
> > That's not the case forRelativeLayout.
> > From my understanding, the two layout should rendre the samething.
>
> > Yes, if we remove "alignParentBottom" to theRelativeLayoutcase, it
> > works, but for what I wan't to do, I really need it.
>
> > So, does anybody knows if it is really abugand maybe there is a
> > workaround or it is the expected behavior and eventually ... why ?
>
> > Thanks a lot !
>
> > Mike
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com
>
> Note: please don't send private questions to me, as I don't have time
> to provide private support.  All such questions should be posted on
> public forums, where I and others can see and answer them
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to