views:

33

answers:

1

This is more out of curiosity than anything else, as I have solved my original problem. I wanted a button the horizontal centre of the screen, with a text box to the left of it, using a Relative Layout. I am curious to know why if I have the following layout:

    <Button android:id="@+id/pickTime"
    android:layout_width="wrap_content"
    android:layout_below="@id/to_button"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"/>

 <TextView android:id="@+id/timetext"
    android:layout_width="wrap_content"
    android:layout_below="@id/to_button"
    android:layout_toLeftOf="@id/pickTime"
    android:layout_alignTop="@id/pickTime"
    android:layout_height="wrap_content"/>

The TextView is not displayed, yet if I subsitute:

  android:layout_toLeftOf="@/id/pickTime"

to

  android:layout_alignParentLeft="true"

It does display (albeit left justified to the left margin rather than right justified to the to the button which I was originally expecting). Is this an issue with all Relative Layouts (i.e always have to define widgets in a set order), or is it just something to do with text needing to be read from the left?

A: 

Check this, it's working:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"&gt;

<Button android:id="@+id/pickTime"
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Button"
    android:layout_height="wrap_content"/>

 <TextView android:id="@+id/timetext"
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@id/pickTime"
    android:layout_alignTop="@id/pickTime"
    android:layout_height="wrap_content"
    android:text="TextView"/>
</RelativeLayout>

I guess you have an issue with android:layout_below="@id/to_button" but you didn't add it and I don't know what is it. :(

Macarse
I think it's something to do with the width of the screen. When I run it with the emulator on HVGA my original code with layout_toLeftOf displays fine, but when I run it on my Hero, or 320 x 480 resolution, it doesn't display. Seems strange, as it appears there is plenty of room
Stev_k
@Stev_k: Paste the whole xml so we can help you out in a more efficient way.
Macarse
@Marcarse it's too long really and besides I think I now know what the problem is - I had the layout_width and layout_height of the Relative Layout as wrap_content, unlike you. Thanks for your answer though, you made me think about it as I went through the steps of testing your code, so you can have the full points!
Stev_k