How to set space between buttons on Android?

Asked

Viewed 4,441 times

1

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

Follows the image as exexmplo:

inserir a descrição da imagem aqui

xml screen:

 <LinearLayout
        android: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> 

2 answers

2


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

Attribute is used layout_weight to scale 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 higher can replace

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

for:

<Space
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
  • you can give a hint, I added another row of buttons has how to give a small space between the top button with the bottom.

  • If you duplicate the xml I posted to represent the new row it will be 15dp below the first. Include the two in a Linearlayout.

  • I made it a button top got stuck on the other bottom

  • The Linearlayout has android:layout_marginTop="15dp" should soon be separated by 15dp

  • 1

    was missing this code, it worked.

2

To add spacing between buttons you can use the 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 with the user screen.

A form: Center 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">

LinearLayout com elementos centralizados e espaçados


Note that the distance between the buttons are the same - 15dp - but is not the same distance between the sides of the phone.

Another way:
Use the layout_weight to define how much % the element will occupy of the parent element. (In this case 1/3 = 0.33333...)
Note that I used a Linearlayout as an intermediate so that the button was not 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>

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.