Align three buttons horizontally in a layout

1

How can I make the bottom buttons line up from left to right instead of top to bottom and occupy the full width of the device that runs it? Can I use percentages?

Deputy code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/background">

<LinearLayout
    android:id="@+id/saldoLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/saldoTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/saldo"
        android:textColor="@color/letra"
        android:textSize="24sp"
        android:textAlignment="center"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/topLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="125dp"
    android:layout_below="@+id/saldoLinearLayout"
    android:orientation="horizontal">

</LinearLayout>

<LinearLayout
    android:id="@+id/midTableLayout"
    android:layout_width="match_parent"
    android:layout_height="355dp"
    android:layout_below="@+id/topLinearLayout"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/topLinearLayout"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/button10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button"
        android:layout_below="@+id/button"
        android:text="Button" />

    <Button
        android:id="@+id/button13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button4"
        android:layout_below="@+id/button4"
        android:text="Button" />

    <Button
        android:id="@+id/button11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_below="@+id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button5"
        android:layout_below="@+id/button5"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button3"
        android:layout_below="@+id/button3"
        android:text="Button" />

    <Button
        android:id="@+id/button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:id="@+id/menuLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/midTableLayout"
    android:orientation="vertical">

    <Button
        android:id="@+id/homeButton"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Home" />

    <Button
        android:id="@+id/noticiasButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Noticias" />

    <Button
        android:id="@+id/estadoButton"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Estado" />
</LinearLayout>

</RelativeLayout>
    
asked by Eduardo 21.04.2017 в 19:06
source

2 answers

0

Try this (it's just the relevant code for the buttons):

<LinearLayout 
    android:id="@+id/menuLinearLayout"
    android:layout_below="@+id/midTableLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

   <Button
        android:id="@+id/homeButton"
        android:text="Botón 1"
        android:layout_weight="1"/>

   <Button
        android:id="@+id/noticiasButton"
        android:text="Botón 2"
        android:layout_weight="1"/>

   <Button
        android:id="@+id/estadoButton"
        android:text="Botón 3"
        android:layout_weight="1"/>

</LinearLayout>
    
answered by 21.04.2017 / 20:57
source
0

It's because those 3 Buttons have a fixed size that in your case is 100dp:

androidlayout_width="100dp"

To occupy the entire width you just have to leave it the same as you have them above:

android:layout_width="match_parent"
    
answered by 21.04.2017 в 19:17