KennetRunner
KennetRunner

Reputation: 301

Android Layout Alignment Issue

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

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

Upvotes: 0

Views: 628

Answers (2)

Virgílio Santos
Virgílio Santos

Reputation: 412

Well, I could just comment Cristian answer, but I don't have enough point, so, here is my version of his layout proposal:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Right" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="In the center" />

</RelativeLayout>

Just removed the align to buttons stuff and put a android:layout_centerHorizontal="true".

Upvotes: 1

Cristian
Cristian

Reputation: 200160

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.

Upvotes: 1

Related Questions