Android Development: Application Layout Richard S. Stansbury 2015
Linear Layout: Fill Default: older layout elements have higher priority when it comes to display real estate Consider the following scenario: – Linear layout with three children – The first child has “wrap_content” – The second child has “match_parent” – Regardless of its configuration, a third child would not have space
Linear Layout: Weight Some people were a bit confused about weight when it came to the last build activity. Details… Weight by default for a component is zero (0) Weight indicates the proportion of the real-estate that a widget gets – Two widgets of equal weight will have equal space. – If widget 1’s weight is 2 and widget 2’s weight is 1, then widget 1 will have twice the space of widget 2.
Linear Layout: Weight Continued Allocating a percentage of space is possible (vertical orientation) – Set heights to zero – Set weights for each component to percentage of real-estate you want to give the component – Weights 50, 20, 30 would result in an widget 1 having 50%, widget 2 20%, and widget 3 having 30%
Gravity Gravity within the widget – Where does the content of the widget align within its borders Gravity within the space of a layout
Relative Layout Supports: – Alignment relative to the container – Alignment relative to other objects XML parameters for alignment within the container (set true or false): – Android:layout_alignParentTop – Android:layout_alignParentBottom – Android:layout_alignParentLeft – Android:layout_alignParentRight – Android:layout_centerInParent – Android:layout_centerHorizontal – Android:layout_centerVertical See also: ayout.LayoutParams.html
Relative Layout (continued) Relative to other widgets – Android. = ” or, ” if first use of Position parameters: android:layout_above android:layout_below android:layout_toLeftOf android:layout_toRightOf
Relative Layout (continued) Alignment Parameters – android:layout_alignTop – android:layout_alignBottom – android:layout_alignLeft – android:layout_alignRight – android:layout_alignBaseline Base line is the implicit line in which text sits – e.g. for two buttons the labels of the buttons would align vertically Processed in two passes so you can align to things that have not yet been declared and vice-versa
Example How would you implement this view? – Components? – Alignments
Relative Layout and Overlap Relative Layout supports overlap with each new item being added to a higher layer in the z direction – i.e. newer stuff sits on top of older stuff Consider a background image with content on top.
Table Layout TableLayout defines a view in which content is divided into rows. – TableRow defines a new row – Content added to the TableRow are divided into columns – Total number of columns for the table is equal to the maximum number of columns across all defined rows – At least one column per widget
Table Layout (cont) parameters – Widgets filled left to right unless a a widget is given an explicit column android:layout_column = “ ” – Where is indexed from zero (0) – Each widget consumes one column – Column span can be set to extended across multiple columns android:layout_span = “ ”
Table Layout Column manipulation – Parameters for a specific column number can be set as a parameter to the TableLayout Stretch – android:stretchColumns – setColumnShrinkable Shrink – android:shrinkColumns – setColumnStretchable() Collapse – Make a column invisible android:collapseColumns setColumnCollapsed()
Example How many rows? How many columns? How do we get the EditText field to span remainder of table? How do we move Reset and Submit to the right?
ScrollView A scroll view can be a layout of layouts. Allows support for smaller devices lacking screen real-estate - vertical scroll - horizontal scroll view (Android 1.5 or later)