Linear layout is a simple layout used in android for layout designing. In the Linear layout all the elements are displayed in linear fashion means all the childs/elements of a linear layout are displayed according to its orientation. The value for orientation property can be either horizontal or vertical.
Types Of Linear Layout Orientation
There are two types of linear layout orientation:
- Vertical
- Horizontal
As the name specified these two orientations are used to arrange there child one after the other, in a line, either vertically or horizontally. Let’s we describe these in detail.
1.Vertical:
In this all the child are arranged vertically in a line one after the other. In below code snippets we have specified orientation “vertical” so the childs/views of this layout are displayed vertically.
2. Horizontal:
In this all the child are arranged horizontally in a line one after the other. In below code snippets we have specified orientation “horizontal” so the childs/views of this layout are displayed horizontally.
Important Note: All of the layout managers can be nested. This means that you can put a Relative Layout or Frame Layout as a child to Linear Layout.
Main Attributes In Linear Layout:
Now let’s we discuss about the attributes that helps us to configure a linear layout and its child controls. Some of the most important attributes you will use with linear layout include:
1. orientation: The orientation attribute used to set the childs/views horizontally or vertically. In Linear layout default orientation is vertical.
Example: Orientation vertical:
Example: Orientation Horizontal:
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of the layout like left, right, center, top, bottom etc.
Example: We have set gravity right for linear layout. So the buttons gets align from right side in Horizontal orientation.
3. layout_weight: The layout weight attribute specify each child control’s relative importance within the parent linear layout.
Example: weight property for button in linear layout. In the below example one button is of weight 2 and other is of weight 1.
In the layout image you can notice Button with weight 2 gets more size related the other.
4. weightSum: weightSum is the sum up of all the child attributes weight. This attribute is required if we define weight property of the childs.
Example: In the same above example of weight, we can define weightSum value 3.
Example of Linear Layout:
Now lets design 2 linear layout UI. First we have designed using weight attribute and second without using it. So below layout output will clear the difference between them:
Example 1: First we will design Android Linear Layout without using weight property
In this example we have used one TextView and 4 Button. The orientation is set to vertical.
Below is the code of activity_main.xml
Output Screen:
0 Comments:
Post a Comment