jul
jul

Reputation: 37464

Buttons with layout_weight="1" take all space in LinearLayout

I want to put 4 buttons horizontally in a linearlayout. In order to space them equally I set android:layout_weight="1" in each button. The problem is that the width of the button is not respected and they fill the whole linearlayout. This works with imageviews, why not with buttons?

Thanks

enter image description here

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="50dp" android:background="#FFFFFF">
    <Button android:id="@+id/btn_sort_pert"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
        android:layout_weight="1" android:text="@string/pertinence"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
    <Button android:id="@+id/btn_sort_dist"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
        android:layout_weight="1" android:text="@string/distance"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
    <Button android:id="@+id/btn_sort_prix"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
        android:layout_weight="1" android:text="@string/prix"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
    <Button android:id="@+id/btn_refine"
        android:layout_width="32dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:layout_weight="1" android:background="@drawable/btn_affiner" />
</LinearLayout>

Upvotes: 0

Views: 3289

Answers (2)

neteinstein
neteinstein

Reputation: 17613

EDIT

You need to add empty views like:

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="50dp" android:background="#FFFFFF">
    <Button android:id="@+id/btn_sort_pert"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
         android:text="@string/pertinence"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
       <View android:layout_width="0dp" android:layout_height="1dp" 
       android:layout_weight="1">
    <Button android:id="@+id/btn_sort_dist"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
         android:text="@string/distance"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
     <View android:layout_width="0dp" android:layout_height="1dp" 
      android:layout_weight="1">
    <Button android:id="@+id/btn_sort_prix"
        android:layout_width="90dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:gravity="center"
        android:text="@string/prix"
        android:textSize="14sp" android:textColor="#FFFFFF"
        android:background="@drawable/btn_sort" />
     <View android:layout_width="0dp" android:layout_height="1dp" 
     android:layout_weight="1">
    <Button android:id="@+id/btn_refine"
        android:layout_width="32dp" android:layout_height="31dp"
        android:layout_gravity="center_vertical" android:background="@drawable/btn_affiner" />
</LinearLayout>

Upvotes: 2

bigstones
bigstones

Reputation: 15257

With those settings the ImageView is behaving like the Button, but you don't see that probably because by default the images will be centered if they're not filling the view in width. It's like a frame, you'll see that with HierarchyViewer (an SDK tool to debug layouts).

If I get what you want, to do the same with buttons you'll have to enclose each button in a layout, so that the enclosing layouts will share evenly the space, and the buttons can stay gravity-centered into those layouts, each with its own width, having a sort of flexible "margin" at their sides. (not tested but should work!)

Upvotes: 2

Related Questions