Wednesday 17 August 2016

LAYOUT





An Android layout is a class that handles arranging the way its children appear on the screen.  Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts.  You could also create your own custom layout by making a class that inherits from ViewGroup.

The standard Layouts are:

1. Linear Layout
2. Relative Layout
3. Table Layout
4. Frame layout
5. Constraint Layout


1. Linear Layout 

In a linear layout, like the name suggests, all the elements are displayed in a linear fashion(below is an example of the linear layouts), either Horizontally or Vertically and this behavior is set in android:orientation which is an attribute of the node LinearLayout.

Example of Vertical layout snippet

<LinearLayout android:orientation="vertical"> .... </LinearLayout>

Example of Horizontal layout snippet
<LinearLayout android:orientation="horizontal"> .... </LinearLayout>


2. Relative Layout

In a relative layout every element arranges itself relative to other elements or a parent element.
As an example, lets consider the layout defined below. The “Cancel” button is placed relatively, to the right of the “Login” button parallely. Here is the code snippet that achieves the mentioned alignment (Right of Login button parallely)


Example code snippet

<Button android:id="@+id/btnLogin" ..></Button>
<Button android:layout_toRightOf="@id/btnLogin"
            android:layout_alignTop="@id/btnLogin" ..></Button>


3. Table Layout

 You can divide your layouts into rows and columns. Its very easy to understand.

4. FrameLayout

FrameLayout is designed to block out an area on the screen to display a single item.

5. Constraint Layout

Allows positioning children relative to each other and the parent. But also offers other powerful positioning and sizing strategies, including horizontal/vertical child "chains" with custom spacing/weighting, arbitrary horizontal/vertical "guidelines," and custom child size aspect ratios.

6. Grid View

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.


Layout Parameter

 To define height and width of view

1. wrap_contect - tells your view to size itself to the dimensions required by its content.
2. match_parent - tells your view to become as big as its parent view group will allow.
3. manual size -  Define size manually in dp(density pixel), dip(density independent pixel), sp(scale pixel) like - 20dp, 12sp




  






No comments:

Post a Comment