There is one LinearLayout with wrap_content layout parameters. I have several views inside it with the match_parent parameter. Let's take a look.
Case 1:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#004400">
</RelativeLayout>
</LinearLayout>
Case 2:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="What a terrible failure!"
android:background="#009900"/>
</LinearLayout>
Case 3:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#000099"/>
</LinearLayout>
Here is the expected behavior. The View is drawn. I can't post third image because of have less then 10 reputation. Thanks stackoverflow! :)
All examples are tested on a real device, not only in android studio preview.
From android Develop -> API Guides -> User Interface -> Layouts: wrap_content - tells your view to size itself to the dimensions required by its content.
Considering the second case. I can still understand that the button has content, and the parent view only accepts button text and padding - it is "content".
But what about the first case? The child clearly says that "I want to be drawn with match_parent". I understand that the RelativeLayout has no children, but it should not bother the parent container or should it?
Or, if Android tries to go down to the lower level by requesting "content size" (OnMeasure), then why do we need to set the width. If it's so clever and decides everything for me?
What exactly is happening here? I do not understand. Why does it heart?