Sunday, March 1, 2015

Android - Linear Layout Learning

Android Linear Layout: 
Linear layout is a ViewGroup that aligns all children in a single direction, vertically or horizontally. The layout direction can be specified in android:orientation attribute. 
All children of LinearLayout are stacked after the other, so a vertical list will only have one child per row no matter how wide they are, and a horizontal list will only e a 
row high, the height of tallest child, plus padding. A LinearLayout respects margins between children and the gravity (right, centre or left alignment) of each child.

Layout Weight 
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns and 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. Child views can specify a weight value and then any remaining space 
in the view group is assigned to the children in the proportion of their declared weight. Default weight is zero. 

For e.g. f there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third field without weight will not grow and will occupy only the area required by its content. 
The other two will expand equally to fill the space remaining after all three fields are measured. If the 3rd filed was given weight as 2, then it is now declared more important than both the others, so it gets harf the remaining space and the first two share the space equally. 

Below example gives an idea of the linear layout. 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="To" />
   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Subject" />
   
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="Message" />
   
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Send" />


the device screenshot for the above is: 


references: 
http://developer.android.com/guide/topics/ui/layout/linear.html

No comments:

Post a Comment