Sunday, 22 March 2020

Difference Between Linear Layout and Relative Layout in Android



Relative Layout:
A relative layout displays its views relative to one another or relative to parent , so order is not that important. You can define the top most view at the end of the layout and provide details to show it on top left. The following attributes are used to define relative layouts:
  1. Position relative to Parent: You can align a view relative to parent using alignParentTop, centerHorizontal etc.
  2. Position relative to other views: You can align a view relative to another view using above, below, toLeftOf etc.
  3. Margins: You can provide margins using marginTop, marginLeft etc.
Linear Layout:
A linear layout displays its views next to each other either vertically or horizontally. So, if you define views in a row, they will be displayed one after the other. You need to specify orientation to define whether layout is vertical or horizontal. The following attributes are used to define linear layouts:
  1. Weight: It specifies how much space each view spans relative to others. For example, in an e-mail application, you can give less weight to ‘To’ and ‘Subject’, and more weight to ‘Message’.
  2. Gravity: It defines placement of a view’s contents. You can decide whether it should be displayed on top, center or bottom.
  3. Layout Gravity: It defines the placement of the view itself.

1 comment: