Widgets and Layouts
Widgets and Layouts
layout_marginRight
1. Margins
layout_marginLeft
Widget
layout_marginBottom
layout_width
• match_parent
• Make my size match my parent’s size
• wrap_content
• Make me just big enough to display my contents
• Hard-coded size (e.g., 30dp)
• px – pixels
• in – inches
• mm – millimeters
• pt – points (1/72 of an inch)
• dp – density-independent pixels, 1/160 of an inch)
• sp – scale-independent pixels (similar to dp, but scaled by user’s font size preference)
LinearLayout
• Arranges children in either vertical column or horizontal row
• Important attributes
• orientation – vertical or horizontal
• gravity – if the children don’t take up all the space inside the LinearLayout, where
should they be placed (center, top, bottom, left, right, etc.)
• How should available empty space inside the LinearLayout be allocated to
the children?
• Put layout_weight attributes on the children indicating what proportion of the
empty space each child would like to have
• layout_weight values are numbers. Absolute values don’t matter, just the
relative sizes of the weights
• layout_weight=”0” means the child does not want any empty space
• layout_height=”0dp” or layout_width=”0dp” means let that
dimension be controlled entirely by the weight
LinearLayout
• Suppose there are three children
• Scenario #1
• If the weights are 0, 0, 0, nobody will get any extra space, and the children
will be positioned according to the LinearLayout’s gravity attribute
• Scenario #2
• If the weights are 1, 1, 1, the empty space will be allocated to the children
equally
• Scenario #3
• If the weights are 0, 1, 0, then the middle child will get all the empty space
and the first and third children won’t get any
GridLayout
• Arranges children in a row/column grid
• Important attributes
• rowCount, columnCount
• Child layout attributes
• layout_row, layout_column – location of child in the grid
• layout_rowSpan, layout_columnSpan – the number of rows and columns
occupied by the child (can span more than one row and one column)
• layout_gravity – if the child doesn’t take up all the space in its group of cells
where should it be placed? (center, top-left, bottom-middle, etc.)
• layout_rowWeight, layout_columnWeight – numbers indicating what
proportion of available empty space should be allocated to this child (in both
vertical and horizontal dimensions)
A
RelativeLayout
B C