5

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>

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 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?

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26

1 Answers1

0

It will not bother. The width of child in first case will be as same as it's parent because you set it match_parent to a wrap_content width parent. And the height of child will be as small/bigger than the parent because you specify an exact value for the height.

Infinite Loops
  • 671
  • 3
  • 11
  • 23
  • About width: in case 3 same condition Height added for other cases, not for first. – Oxota Za Pivom Jun 24 '17 at 06:36
  • All cases are same as case 3. As you stated. – Infinite Loops Jun 24 '17 at 10:10
  • i just mean that height Inserted for clarity in case 2,3. It is not important. And about the width, you did not answer anything new. I myself see these parameters. And i know what it's mean. I am confused because I can not understand the logic of these examples in the aggregate. They contradict each other. – Oxota Za Pivom Jun 24 '17 at 10:26
  • I can accept the argument about the empty content of RelativeLaout. But Button and View have differences(why? overriding onMeasure effects?). I know that Button IS-A TextView IS-A View. There is screen for case 3 [link](http://i.imgur.com/l1SGXU2.png) – Oxota Za Pivom Jun 24 '17 at 10:37