Android: simple question of LinearLayout and fill_parent

I am sure that I am missing something simple ...

Background:

I'm new to Android and UI design, and I just wanted to play around with layouts. Right now I want to check the box above the text label. I use the following xml layout that works fine:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <CheckBox android:id="@+id/check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/check_box_text" /> <TextView android:id="@+id/dummy_display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dummy_label" /> </LinearLayout> 

My tests (just to understand how everything works):

  • Scenario 1) When I set the CheckBox layout_height to "fill_parent" , the CheckBox occupies the entire screen and is centered (that is, the TextView disappears).

  • Scenario 2) If instead of TextView layout_height "fill_parent" set CheckBox , then CheckBox will disappear NOT . Nothing actually disappears, and the layout looks the same as higher than xml, where everything fits in the top left.

Question (and comments):

Why does scenario 1 work the way it works?

This behavior seems inconsistent to me. I thought fill_parent should only allow the element to fill all the free space in the parent. Thus, it seems to me that the TextView should get any necessary space (since it is wrap_content ), but the CheckBox should occupy the rest of the space (so the TextView will be forced to the bottom of the screen, but not invisible). In other words ... Scenario 2 makes sense to me, but Scenario 1 does not.

Please explain: -).

Thanks,

Tom

+6
android user-interface xml layout
source share
1 answer

LinearLayout measures it as one step at a time, so in the first example he sees that the first child wants to have all the available height that he gives him. In the second example, it provides all the available space to the second child. If you want the flag to occupy all the space except the space required for the text field, you must change the layout_height from the flag to wrap_parent and set the layout_weight property for each child element, the weight of the layout shows how much of the available space after all the children have been measured, should be added to each of them. To be more specific, how it works in steps:
1) The LinearLayout parent tells the linear layout how large it can be 2) LinearLayout asks all the children to set their layout and allocate available space.
3) If any free space is still available, it distributes it among the children according to their weight (max. Weight between 0 and 1, 0 - by default)
Hope this helps Edit:
I checked the source code of LinearLayout and found out that if the line layout had FILL_PARENT layout parameters and not a zero weight measurement code, set the layout height to WRAP_CONTENT , and then distribute the space between all the child elements according to the weights.

+13
source share

All Articles