Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: strange weight inversion in LinearLayout

This is the xml layout i'm working on:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ScrollView 
        android:layout_weight="2" 
        android:id="@+id/scrollConfirm" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

  </ScrollView>

  <LinearLayout 
      android:layout_marginTop="20px"
      android:layout_weight="1" 
      android:id="@+id/imageNumpad" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent">
        <ImageView android:src="@drawable/myicon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
        />
   </LinearLayout>
</LinearLayout>

Since I have setted the ScrollView with layout_weight="2" and the LinearLayout (child) with layout_weight="1" I expected that the ScrollView will use up twice free space that the LinearLayout. But I obtain the opposite result. The ScrollView is smaller than LinearLayout. Whereas if I set for the ScrollView layout_weight="1" and LinearLayout with layout_weight="2", the ScrollView is greater than LinearLayou.

How is this possible??

like image 401
GVillani82 Avatar asked Apr 18 '13 15:04

GVillani82


People also ask

What is layout_ weight in android?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

How to use weight in Linear layout?

Weight can only be used in LinearLayout . If the orientation of linearlayout is Vertical, then use android:layout_height="0dp" and if the orientation is horizontal, then use android:layout_width = "0dp" . It'll work perfectly.

What is the default orientation of a LinearLayout?

When the orientation of a LinearLayout is unspecified, it is using the default, which is horizontal . Should the layout be a column or a row? Use "horizontal" for a row, "vertical" for a column. The default is horizontal.

When we use Linear layout?

Android Layout Types Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.


1 Answers

Weight get inverse because you are using match_parent as the layout_height. The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout. Set your heights to 0dip it will work.

i.e., set layout_height = "0dip" for both ScrollView and inner LinearLayout.

Reference :

The use of layout_weight with Android layouts

like image 193
Sankar V Avatar answered Nov 15 '22 16:11

Sankar V