tags:

views:

68

answers:

1

I am trying to find the correct xml to define a layout of three items on a single 'line' as follows :

  • Button (left aligned on the screen) with the text 'Prev' - wrap-context
  • TextView (center aligned) with the title of the screen - fixed 200px size
  • Button (right aligned on the screen) with the text 'Next' - wrap_content

Just cannot seem to get it working unless I use absolute layout which I'd rather not. I have experimented (seems like every option) with gravity/layout_gravity and embedded LinearLayouts...

I guess the question is - can I right align one item, left align another and center align another all on the same LinearLayout 'line'

Thanks .. KJ

+1  A: 

In that case you better use RelativeLayout as follows:

<?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">
    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Left" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_left"
        android:layout_alignTop="@id/btn_left"
        android:text="In the center" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Right" />
</RelativeLayout>

By the way... it's not recommended to give exact dimensions to your Views. But rather, use relative things like fill_parent, wrap_layout and the like.

Cristian
Cristian, Thanks for the response but this didn't seem to work for me - I think the TextView needs to have android:layout_toRightOf instead of _toLeftOf...Even so the TextView is never center aligned - it is left aligned directly after the left button.
Ken Hughes