ViewGroup match_parent inside wrap_content

There is one LinearLayout with wrap_content layout options. I have multiple views inside it with match_parent parameter. Let's watch.

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>

      

enter image description here

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>

      

enter image description here

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's the expected behavior. The view is drawn. I am unable to post the third image due to less than 10 reputation. Thanks stackoverflow! :)

All examples are tested on a real device, not just in Android Studio Preview.

From android Develop -> API Guides -> User Interface -> Layouts: wrap_content - indicates your size to fit the dimensions required for its content.

Considering the second case. I can still figure out that the button has content and the parent view only accepts the button text and padding is "content".

But what about the first case? The child clearly says "I want to be drawn with match_parent". I understand that RelativeLayout has no children, but it shouldn't bother the parent container or is it?

Or, if Android is trying to downgrade by asking for "content size" (OnMeasure), then why do we need to set the width. If he's so smart and decides everything for me?

What exactly is going on here? I do not understand. Why is this heart?

+3


source to share


1 answer


It doesn't bother. The width of the child in the first case will be the same as the parent's, since you are setting match_parent to the parent's wrap_content's width. And the height of the child will be less / more than the parent because you are giving an exact value for the height.



0


source







All Articles