android developer
android developer

Reputation: 116382

RelativeLayout takes all its space, even though I've set its height to "wrap_content"

Background

I have a RelativeLayout inside a FrameLayout/RelativeLayout (doesn't matter to me), which should be at the bottom of the screen (like a toolbar), and should hold a few views in it.

Its height is set to "wrap_content" and so does its child-views.

The child-views of this layout are : A textView that is on the left, and a Horizontal LinearLayout on the right with a few buttons.

The problem

It seems that no matter what I do, the textview is causing the RelativeLayout to take the whole space, instead of just its children.

The code

Here's the minimal XML content that causes this problem. I've removed the extra stuff (LinearLayout and its children, and also some attributes that don't matter) since they don't cause this problem in case I remove the TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000" >

    <!-- Here I've put some views that don't have any relation with the views below, so it doesn't have anything with do with the problem -->

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/vertical_gradient_transparent_to_black" >

        <!-- Here I've put a LinearLayout that doesn't cause the problem, so I've removed it for simplicity-->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>

</RelativeLayout>

I've tried many possible attributes, and also tried adding additional layouts to try to "fool" the RelativeLayout, but none of those have succeeded.

The question

Why does it occur?

A working solution would be to use a Horizontal LinearLayout (with a weight for the TextView ) instead RelativeLayout , but I still want to know why can't I use a RelativeLayout, and why it occurs. Also how to fix it while still using RelativeLayout.

Upvotes: 2

Views: 1545

Answers (3)

user6011162
user6011162

Reputation:

From the RelativeLayout doc:

Class Overview

A Layout where the positions of the children can be described in relation to each other or to the parent.

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM

Class documentation

Which is exactly your case. RelativeLayout can not do that.

Upvotes: 5

Michael Shrestha
Michael Shrestha

Reputation: 2555

This is some kind of Strange issue. Maybe someone else has the explanation.

I found it working when i removed the line

android:layout_alignParentBottom="true"/>

and it also worked when i tried some like this

<TextView
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"/>

Upvotes: 0

suitianshi
suitianshi

Reputation: 3340

I have encountered the same issue before, this might be caused by android:layout_alignParentBottom attribute.Maybe you can find another way to achieve your desired effect. See this for more information.

Upvotes: 0

Related Questions