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

on Tuesday, September 9, 2014


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?


When I use a Horizontal LinearLayout (with a weight for the TextView ) instead RelativeLayout , it works fine...


0 comments:

Post a Comment