Android layout_weight comportment

I am trying to build an android layout using layout_weight to fit all device sizes, and I have some problems understanding its correspondence.

I noticed that changing layout_width / layout_height affected layout_weight , but I don't understand how to do this.

Say, for example, I want to have a vertical LinearLayout divided by three inches LinearLayout , such as the upper and lower parts fill 25% of the screen, and in the middle 50%, and this should not depend on the contents of the internal layouts. (If the contents of the inner LinearLayout too large, it should not offset other layouts)

To do this, should I set the layout_height attribute of the inner LinearLayout to fill_parent or to wrap_content ?

Thanks!

EDIT: It looks like layout_weight is inversely proportional to the size of the layout that will occupy.

3 examples:

Weight 1/1/1 (works as I expected)

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#FF0000"/> //RED <LinearLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00FF00"/> //GREEN <LinearLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#0000FF"/> //BLUE </LinearLayout> 

Results: enter image description here

Weight 1/2/1 (Why, oh why?)

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#FF0000"/> //RED <LinearLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00FF00"/> //GREEN <LinearLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#0000FF"/> //BLUE </LinearLayout> 

Results: enter image description here

** Weight 3/2/3 (What I wanted to do with 1/2/1):

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="3" android:background="#FF0000"/> //RED <LinearLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00FF00"/> //GREEN <LinearLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000FF"/> //BLUE </LinearLayout> 

Results: enter image description here

+4
source share
3 answers

You must set the layout_height of all three internal layouts to "fill_parent", and then change their weights so that they look the way you want.

+1
source

You can try setting layout_height = 0dp

+3
source

In order for layout_weight work properly (and documented), you must not set layout_height either fill_parent or match_parent .

The example on the right at http://developer.android.com/guide/topics/ui/layout/linear.html#Weight shows an example where layout_height set to 0dp . However, not setting it at all or not setting it to wrap_content also does layout_weight work as expected.

+3
source

All Articles