Align buttons horizontally at equal intervals in linearlayout

I am trying to make a linear layout with 5 buttons arranged horizontally at equal intervals, but all the sizes (width) of the buttons should only be 40dp.

I tried this:

<LinearLayout   android:id="@+id/button_layout"
                    android:background="#DCE1DC"
                    android:orientation="horizontal"
                    android:weightSum="5"
                    android:layout_width="fill_parent"
                    android:layout_height="70dip">

        <Button     android:id="@+id/button_A"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_B"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_C"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_D"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="30dp"/>

        <Button     android:id="@+id/button_E"
                    android:layout_weight="1"
                    android:layout_height="60dp"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="30dp"/>

    </LinearLayout>

its work, but I need the width of the buttons to be less, how to do it?

+5
source share
2 answers

For each button, change this:

android:layout_width="0dp"

For this:

android:layout_width="40dp"
+1
source

I think this will solve your request ...

as you say, you use Linearlayout, then you can do something like ...

<LinearLayout
android:layout_width="match_parent"
android:layout_height="yourheight"
android:orientation="horizontal">
<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />

//add as many buttons as you want 


<Button 
android:id="@+id/button_name"
android:weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout> 
+1
source

All Articles