Search code examples
androidmarginandroid-relativelayout

Why is layout_marginBottom ignored when using wrap_content?


I have the following layout file:

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

<RelativeLayout
    android:id="@+id/usericon_img"
    android:layout_width="73.3334dp"
    android:layout_height="70.6667dp"
    android:layout_marginBottom="2.6667dp"
    android:layout_marginLeft="3.3334dp"
    android:layout_marginTop="2.6667dp"
    android:background="#FFFFFF" />
</RelativeLayout>

On the emulator (I've tested it on a real device and it looks the same) the layout looks like this:

My question is: why I don't have a red margin under the white layout? I know that if I change the outer layout to:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="76dp"
android:background="#FF0000" >

I'll get what I want, but why doesn't wrap_content work properly?


Solution

  • In the documentation:

    To measure its dimensions, a view takes into account its padding.

    If I am not mistaken it means that margin is not added to the dimension. That means wrap_content works correctly but don't take margin into account.

    Just a short question: Why do you use values like 70.6667dp?