Jonathan Silva
Jonathan Silva

Reputation: 136

How to prevent relativeLayout from rising with linearLayout

I need the icon to remain in the bottom corner of the screen

When clicking on the icon, a RelativeLayout is displayed, but the icon goes up along with it

Prints

OK

enter image description here

Problem:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:src="@mipmap/ic_launcher"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/close"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_marginStart="35dp"
            android:src="@drawable/ic_close"
            tools:ignore="ContentDescription" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="400dp"
        android:layout_height="250dp"
        android:layout_marginStart="50dp"
        android:background="#000000"
        android:orientation="vertical"
        android:visibility="gone">

        <!-- TODO -->

    </LinearLayout>
</FrameLayout>

Upvotes: 0

Views: 116

Answers (2)

Surender Kumar
Surender Kumar

Reputation: 1123

You have to use gravity. Try this layout:

<RelativeLayout
    android:layout_gravity="bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@mipmap/ic_launcher"
        tools:ignore="ContentDescription" />

    <ImageView
        android:id="@+id/close"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:layout_marginStart="35dp"
        android:src="@drawable/ic_close"
        tools:ignore="ContentDescription" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/content"
    android:layout_width="400dp"
    android:layout_height="250dp"
    android:layout_marginStart="50dp"
    android:background="#000000"
    android:orientation="vertical"
    android:visibility="visible">

    <!-- TODO -->

</LinearLayout>

Hope it will help.

Upvotes: 0

cribetti
cribetti

Reputation: 44

You have a parent FrameLayout containing a RelativeLayout and LinearLayout. A FrameLayout simply places layouts inside on top of each other, although child layouts can be positioned with layout_gravity. In general, it can get a bit crazy when you mix RelativeLayout and LinearLayout, although you can do it as long as you understand how they will work together.

There's a few ways you can make your child layouts behave the way you want in your question, but to make it quick and simple, use a parent RelativeLayout rather than FrameLayout, and have the child views position themselves inside the parent layout with the one of the many layout_alignXYZ attributes. (see: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams) I hope this helps out.

Upvotes: 1

Related Questions