views:

272

answers:

4

If I have 2 LinearLayouts split %50/%50 everything is fine. Weights are 1 and 1. As soon as I add a TextView inside the top LinearLayout, it stretches that layout. I use "wrap_content" as the documentation says I should when it comes to weights. As you can see the red and green should be split evenly and text on grey background should be inside red box. Here is the code:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff0000"
>
 <TextView
 android:text="@string/hello"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="#cccccc"
 android:textColor="#000000"/>      
</LinearLayout>


<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"
>
</LinearLayout>    

Now if I switch to "fill parent" as follows it actually works but it creates another problem. Here is the code (so far so good):


So looking at above we were forced to use "fill_parent" and we would think like we fixed the problem but here is the problem if we are using "fill_parent" (I took out the textview just to show the problem, textview doesnt make the problem go away anyways):

As you can see I assign the weights 3 (top red) and 2 (bottom green) but what actually happens is they get flipped: The red becomes 2 and bottom becomes 3. Just measure the pixels too see.

Here are the results of the 3 codes:

http://imgur.com/iVt8g.jpg

Anyone had some similar experiences?

NOTE: Just to be clear, every single time the layout was wrapped inside this (the top layout): android:layout_width="fill_parent" android:layout_height="fill_parent" with hxml version="1.0" encoding="utf-8" and proper namespace. I don't know why it doesnt come out properly when I try to paste it here. It just wasn't pasting correctly so I took it out.

A: 

Now if I switch to "fill parent" as follows it actually works but it creates another problem. Here is the code (so far so good):

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff0000"
>
    <TextView
    android:text="@string/hello"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#cccccc"
    android:textColor="#000000"/>           
</LinearLayout>


<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
>
</LinearLayout>    
Droidamature
A: 

So looking at above we were forced to use "fill_parent" and we would think like we fixed the problem but here is the problem if we are using "fill_parent" (I took out the textview just to show the problem, textview doesnt make the problem go away anyways):

As you can see I assign the weights 3 (top red) and 2 (bottom green) but what actually happens is they get flipped: The red becomes 2 and bottom becomes 3. Just measure the pixels too see.

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#ff0000"
>
</LinearLayout>


<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00ff00"
>
</LinearLayout>    

As you can see I assign the weights 3 (top red) and 2 (bottom green) but what actually happens is they get flipped: The red becomes 2 and bottom becomes 3. Just measure the pixels too see.

Here are the results of the 3 codes:

http://imgur.com/iVt8g.jpg

Droidamature
A: 

Sorry about the broken post. I split it into 3 parts. First part I had the 3 separate codes, but it didn't come out in this stackoverflow WYSIWYG editor here....

Droidamature
A: 

Try adding a child view to the bottom layout.

Konstantin Burov