0

I am trying to arrange two buttons in an horizontal LinearLayout so that they are evenly distributed, but at the same time the buttons do not stretch over the available space (just has empty space in between).

I am trying to follow what I found in this question, especially the comment from Andrew...

My current layout looks like this.

 <?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="horizontal"
        android:background="#ffffff"
        android:minHeight="?android:attr/listPreferredItemHeight">
    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            >
        <Button
                android:id="@+id/db_export_cancel"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Cancel"
                />
    </LinearLayout>
    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            >
        <Button
                android:id="@+id/db_export_ok"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="OK"
                />
    </LinearLayout>
</LinearLayout>

The two LinearLayouts are properly using each half of the available space, but the buttons are aligned to the left in each of them...

Community
  • 1
  • 1
Matthieu
  • 16,103
  • 10
  • 59
  • 86

2 Answers2

1

Change each of the inner LinearLayouts to have vertical orientation. Each should then pay attention to the horizontal gravity of its enclosed Button.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

What's the point of those LinearLayouts around each button? Just set the weight on the individual buttons and get rid of the middlemen. Then, if you want to make the buttons smaller you can either a) add some left and right padding to the buttons themselves, or b) add two spacer views to the left and right of the buttons, each with a weight of 1 as well.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Without those LinearLayouts, the buttons will take all the space, the point is to have the buttons with the same size only distributed over the space available. With padding or spacer views, I don't think it would be possible to distribute those buttons like I want to... (especially if they have different sizes like here) – Matthieu Dec 12 '11 at 23:07
  • If you set the weight attribute directly on the buttons, they will *not* have different sizes, they will both be the same size. Which, might be undesirable to you, but personally I like evenly sized buttons, especially when they're "ok" and "cancel" (for example, in dialogs). It's more consistent. – dmon Dec 12 '11 at 23:10