Sunday, 5 July 2020

What is Linear Layout in Android ? And how it works ?

In last post we discussed what are layouts in android and saw various names of layouts that android provides. In this post we will see what is Linear Layout and How it works ( or how we can make designs in android using this layout ).

Linear Layout as the name is suggesting us that it aligns the views in linear fashion. This layout aligns its children in either direction such as horizontal or vertical. You can set the direction (horizontal or vertical) of layout by using 
android:orientation attribute. Let us see diagrammatically how linear layout align its children : 



     
                           
view 1


view 2 
                          
         view 3             view 4         

                                       Fig 0.1 Orientation : Horizontal

        


  
               view 1                              
view 2    
              view 3
              view 4


                                       Fig 0.2 Orientation : Vertical

Now let us see some attributes of linear layout : 

    
 * android:orientation (value = horizontal or vertical)
  
   It will arrange the children in either horizontal or vertical direction.

android:gravity  (value = center|top|bottom|left|right) 
 
  It will position the child in center or top or left or right or topleft(top|left) or topright(top|right) etc.

android:weightSum   (value = any positive integer value such as 1 or 2 or 3 ..so on)

      It defines in how many section or division will linear layout be divided. It affects the value of
 android:layout_weight. Following example will explain it how : 

 if linear layout has given weightSum of 3 , it means it should be divided in three sections,
  now if linear layout has three child such as 3 textviews then if their layout_weight is 1 for 
  each then all textviews will be divided into equal sections. 

 if one of the textview's layout_weight is 2 then it will have twice the section then other two textviews.
 It is not compulsory to provide weightSum to the linear layout. But if you provide layout_weight to its children 
 they will work accordingly. 





 



No comments:

Post a Comment