SyntaxHighlighter

Wednesday, September 22, 2010

Android Programming Jumpstart – 5

image This is lab five in my ongoing series of jumpstart Android programming tutorials.
This lab will focus on containers.






We need containers to better organize our widgets
  • LinearLayout
    • Use to lineup contents in rows or columns
    • :orientation property - set the value to be horizontal for a row or vertical for a column.
    • Fill up the space – widgets inside a LinearLayout must set layout_width and layout_height, by a dimension, wrap_content, or fill_parent
    • :layout_weight property – sets what proportion of the free space a widget gets. If there are two widgets and you set one’s weight to 1 and the other one’s to 2, the second will get twice as much of the free space.
    • :layout_gravity – alignment - default for LinearLayout is align top left.
      • For LL columns values are left, center_horizontal, and right
      • For LL rows – default to let the widgets align along their bottom, but you could use center_vertical to center along the row’s vertical midpoint
    • :padding property – use this to insert whitespace between widgets. Using this property will add padding around the widget equally.
      • If you need the padding to vary, use android:padding<Left, Right, Top, Bottom>
  • RelativeLayout
    • Contents are laid out based on their relationship to the other widgets and the parent container
    • Alignment is based on total size – includes padding if any
    • Set relationships to parent container using:
      • android:layout_alignParrent<Top, Bottom, Left, Right>
      • android:layout_center<Horizontal, Vertical, InParent>
      • These are set using true or false
    • Set Relationships to other widgets using:
      • Referenced widgets need to have an id tag and should be referenced using “@id”
      • android:layout_<above, below, toLeftOf, toRightOf>
      • android:layout_align<Top, Bottom, Left, Right, Baseline>
      • android:layout_toRightOf = "@id/widget_a"
image
image
  • TableLayout
    • Layout a grid just like HTML tables
    • TableLayout controls the overall characteristics of the container
    • Your widgets go into TableRow containers
    • Columns are controlled less directly than in HTML
      • Think one column per widget in your widest row
      • Widgets can span columns using android:layout_span
      • Fills left to right or specify the number using android:layout_column
      • Like your wallet – zero based numbering
      • Confusingly, Android will let you place widgets between rows without being in a TableRow container, but its width will automatically be set to fill_parent
      • Columns size themselves to the width of the widest widget in the column, but you can control this behavior by setting properties on the TableLayout itself using:
    • android:<stretchColumns, shrinkColumns, collapseColumns>
    • These take a column number or a comma separated list of column numbers
  • Scrollview
    • Given that the Android device will have limited screen area, at some point you will have to use scrolling
    • Can only have one child element – but that child can have many elements
    • There is a HorizontalScrollView, but generally users mind scrolling up and down less than side to side
image

Some sample code
image

No comments:

Post a Comment