how to set space between buttons on Android?

0

I'm using 3 buttons on a Linear Layout . I would like to distribute the spaces between the buttons.

Follow the image as exexmplo:

Screenxml:

<LinearLayoutandroid:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="texto1"
            android:id="@+id/btn1"
            android:onClick="botao1"
            android:textSize="10dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="texto2"
            android:id="@+id/btn2"
            android:onClick="botao2"
            android:textSize="10dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="texto3"
            android:id="@+id/btn3"
            android:onClick="botao3"
            android:textSize="10dp" />

    </LinearLayout> 
    
asked by anonymous 20.03.2016 / 21:01

2 answers

1

In addition to the buttons you will need to use an view "empty" (1) to represent the spaces before, between and after each button.

layout_weight attribute is used to scale the spaces equally.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="15dp">

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="texto1"
        android:id="@+id/btn1"
        android:onClick="botao1"
        android:textSize="10dp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="texto2"
        android:id="@+id/btn2"
        android:onClick="botao2"
        android:textSize="10dp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="texto3"
        android:id="@+id/btn3"
        android:onClick="botao3"
        android:textSize="10dp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"/>
</LinearLayout>

(1) If minSdkVersion is equal to 14 or greater you can replace

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1"/>

by:

<Space
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
    
20.03.2016 / 22:40
1

To add spacing between the buttons you can use layout_margin :

android:layout_margin="10dp" 
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"

However, there are several ways for them to be equally spaced and allied to the user's screen.

One way: Center the Linearlayout elements and place margins on the second button

<LinearLayout
        ...
        android:gravity="center">
        ...
        <Button
            ...
            android:id="@+id/btn2"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp">


Seethatthedistancebetweenthebuttonsisthesame-15dp-butitisnotthesamedistancebetweenthesidesofthecellphone.

Anotherway:
Use layout_weight to set how many% the element will go occupy the parent element. (In this case 1/3 = 0.33333 ...)
Note that I used a LinearLayout as an intermediary so that the button would not be stretched, but LinearLayout.

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight=".333333333">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="texto1"
                android:id="@+id/btn1"
                android:onClick="botao1"
                android:textSize="10dp"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight=".333333333">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="texto2"
                android:id="@+id/btn2"
                android:onClick="botao2"
                android:textSize="10dp"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight=".333333333">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="texto3"
                android:id="@+id/btn3"
                android:onClick="botao3"
                android:textSize="10dp"/>
        </LinearLayout>

    
20.03.2016 / 23:28