Saturday, 20 April 2013

Linear Layout and its major properties in android


[1] LinearLayout
  • LinearLayout is a view group that align all children in a single direction, vertically or horizontally. The default orientation is horizontal.
    In vertical LinearLayout,all children are stacked one after another,so a vertical list will only have one child per row,no matter how wide they are and a horizontal list will only be one row high. You can specify the layout direction with the android:orientation attribute or setOrientation() method.

    Gravity: which specifies the alignment of all the child elements(right, center,or left alignment) by calling setGravity() method or android:gravity  attribute.

    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 is 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 children in the proportion of their declared weight. Default weight is zero.
              
                  
                 




    Another important property of linear layout is Orientation.
    Orientation is either Horizontal or vertical.
    If Orientation is horizontal then it adds view in one row and if Orientation
    is vertical then it adds view in one column.
    Below xml is for horizontal orientation



    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/personinformation_txtvw_title1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />
     <TextView

                android:id="@+id/personinformation_txtvw_title2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />
     <TextView

                android:id="@+id/personinformation_txtvw_title3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />


     </LinearLayout>


    For vertical orientation you have to just change orientation="Vertical" in the linear layout .
    Below xml is for Vertical orientation



    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/personinformation_txtvw_title1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />
     <TextView

                android:id="@+id/personinformation_txtvw_title2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />
     <TextView

                android:id="@+id/personinformation_txtvw_title3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/title" />


     </LinearLayout>



    There is another useful property of linear layout is layout:weight.
    It will assign the area of view.

    Hope this will clear the linear layout and you can use it friendly in your android application.







No comments:

Post a Comment