jakehschwartz
jakehschwartz

Reputation: 1005

Android layout alignment issues

So I am trying to get my layout to look like this:

| button       text       button |

So that the text is centered between my two buttons. I first tried using LinearLayouts and layout_gravity tags, but that would give me something like:

|        buttontextbutton        |

and now I have a relative layout with alignParentLeft/Right and centerInParent tags but the layout now looks like

| button text buttonnnnnnnnnnnnn |

Is there a way to get my layout right with text that changes lengths?

Thanks, Jake

Upvotes: 1

Views: 1006

Answers (3)

user432209
user432209

Reputation: 20167

Use a linear layout with a horizontal orientation setting. Remove any gravity/align settings. Next use the line

android:layout_weight="1"

in both buttons and the textview. That should give you what you want.

Edit: You asked how it works. This is from the developer doc

LinearLayout also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three boxes are measured. If the third box is then given a weight of 2 (instead of 0), then it is now declared "more important" than both the others, so it gets half the total remaining space, while the first two share the rest equally.

Upvotes: 3

Vignesh Mukund
Vignesh Mukund

Reputation: 31

I'd suggest you try out <TableLayout> for this. Make sure you set android:stretchColumns="0,1,2" in your case.

<TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:stretchColumns="0,1,2">

Upvotes: 0

Kevin Coppock
Kevin Coppock

Reputation: 134664

You should be able to do this with a LinearLayout just fine. Try this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView Centered"
        android:gravity="center"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        />
</LinearLayout>

I'm not certain that the layout_weight attribute is even necessary in this case. You can try it with or without, but this should do the trick.

Upvotes: 0

Related Questions