Skip to content Skip to sidebar Skip to footer

How To Put 2 Layouts On Top Of Each Others

I'm trying to put 2 layouts on top of each others but it doesn't work.There is a space that i don't know how to remove. here is my code:

Solution 1:

Use Relative layout to place views on top of each other , like this

<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|center"android:background="#f00"android:orientation="vertical"android:weightSum="1" ><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="170dp"android:layout_centerInParent="true"android:layout_gravity="bottom|center"android:background="#fff"android:gravity="bottom"android:orientation="vertical" ><LinearLayoutandroid:layout_width="10dp"android:layout_height="100sp"android:layout_centerInParent="true"android:layout_marginLeft="10sp"android:background="#37c100" ></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="6sp"android:background="@drawable/arrow_shape" ></LinearLayout></RelativeLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="150dp"android:layout_centerInParent="true"android:layout_margin="25sp"android:layout_weight="0.5"android:background="#0000ff"android:gravity="center" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="40 000 "android:textColor="#ffffff"android:textSize="25sp" /></LinearLayout></RelativeLayout>

Solution 2:

This can now also be done using a constraintlayout. In my use case I had a RecyclerView that when empty had to be "covered" by a text and a button to allow users to add items to the list. (Part of the Big Nerd Ranch programming book, challenge 13.3)

Code is as follows (change to your own needs):

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/no_crimes_container"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><TextViewandroid:id="@+id/no_crimes_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="@string/no_crimes"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="@+id/recyclerView"app:layout_constraintStart_toStartOf="@+id/recyclerView"app:layout_constraintTop_toTopOf="@+id/recyclerView" /><Buttonandroid:id="@+id/new_crime_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="@string/new_crime"app:layout_constraintBottom_toBottomOf="@+id/recyclerView"app:layout_constraintEnd_toEndOf="@+id/recyclerView"app:layout_constraintStart_toStartOf="@+id/recyclerView"app:layout_constraintTop_toBottomOf="@+id/textView" /></LinearLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/crime_recyclerview"android:layout_width="match_parent"android:layout_height="match_parent" /></android.support.constraint.ConstraintLayout>

Solution 3:

In your second LinearLayout, you have a android:layout_margin="25sp". Remove this line or at least don't set a marginTop.

Post a Comment for "How To Put 2 Layouts On Top Of Each Others"