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?
wrap_content is used to wrap the view according to its actual size. It can be used with layout_height as well as layout_width. If we set layout_height as wrap_content then the height will be the same as the content size and if we set layout_width as wrap_content then the width will be the same as the content size.
wrap_content — The component just want to display big enough to enclose its content only.
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With