Position button at bottom of layout

1

I would like to position the Exit button at the end of my layout. I'm using LinearLayout. Any tips on how to position?

This is the Linear Layout code:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.meuprojeto.perfilloginsenha.PerfilActivity">

This is the button code:

<Button
    android:id="@+id/btnLogOut"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:text="SAIR"
    android:textColor="@color/branco"
    android:textSize="20dp"
    android:textStyle="bold" />

    
asked by anonymous 21.12.2017 / 02:09

1 answer

1

Contrary to what you might think, in a LinearLayout, match_parent does not make it occupy all parent . The old name, fill_parent , in that sense, was even worse.

Place a view before the button so that it occupies all the space left over from the layout. This will force the button to be positioned at the bottom.

In order for a view to occupy all remaining available space, it must be assigned a layout_weight .

Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Botão"/>
</LinearLayout>

An alternative is to use a RelativeLayout and add android:layout_alignParentBottom="true" to the button:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Botão"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>
    
21.12.2017 / 14:47