How to make Linearlayout stay at the bottom of the screen?

Asked

Viewed 698 times

2

I’ve tried many things here to make my Linearlayout stand at the bottom of the screen but I can’t do that. I need to do this with Linear which has id= "underneath" Does anyone know?

Follows my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="Teste"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

    <LinearLayout
        android:id="+id/embaixo"
        android:layout_gravity="end"
        android:gravity="end"
        android:padding="10dp"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/marca_simplifica" />
    </LinearLayout>
</LinearLayout>

  • 1

    Hello @Aline, ever considered using weights ? If they don’t fit, you can paste the linear layout inside a relativelayout and throw it to the bottom.

  • How????????? Will I try to do it here

  • 1

    Did you manage to do Aline ? I gave an answer below, maybe serve as an example for your problem

  • I did. But as a beginner I understood more the other answer..........

  • 1

    Ah is very simple haha say that there I have 2 linearlayout inside a relativelayout, one will stay at the top and one at the bottom of the screen

  • Ah understood now Matheus :)

  • Soon I’ll ask another question Matheus... Because I’m having trouble getting the button back on a Tablayout... :/

Show 2 more comments

2 answers

1


The linearlayout let’s say they respect a hierarchy, if they are horizontal then they will be placed next to each other, if they are vertical they will be placed one below the other. The order is XML compliant. The relativelayout, let’s say break this pattern and you can adapt other layouts according to your need.

Below is an example of how to put a linearlayout on top and a bottom on a relativelayout:

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

    <LinearLayout
        android:id="@+id/top_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>
</RelativeLayout>

0

You must include the android:layout_alignParentBottom="true", but for the same to work, you have to be the son of a RelativeLayout example:

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

        <LinearLayout
             android:id="+id/embaixo"
             android:layout_alignParentBottom="true
             android:gravity="end"
             android:padding="10dp"
             android:background="@color/colorAccent"
             android:orientation="horizontal"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

             ...            

        </LinearLayout>

    </RelativeLayout>
  • This answer also works but it is necessary to place the Linearlayout inside a Relativelayout. :)

Browser other questions tagged

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