Open main menu

CDOT Wiki β

Changes

MAP524/DPS924 Lecture 4

2,121 bytes added, 19:42, 12 July 2015
Created page with '= Layouts = Layouts are a way to arrange your widgets on the screen semi-automatically, so that your app looks normal regardless of screen size and resolution, android version, …'
= Layouts =

Layouts are a way to arrange your widgets on the screen semi-automatically, so that your app looks normal regardless of screen size and resolution, android version, theme in use, font size, etc.

In most cases you will not be referencing your layout from code, since Android will handle everything you'd expect a layout manager to do. It's the contents of the layout that you care about.

No matter which layout you choose to use at first it will take you time to figure out the combination of android:layout_* attributes to make it look exactly as you want. We'll try a few experiments in class today, and you should spend a few hours playing with them also.

== LinearLayout ==

Puts all its contents in one line: either horisontal or vertical. Though very simple this can be used to create nearly any kind of layout you like, especially since you can put one layout inside another one.

You can use layout_weight to decide how to allocate space to each View in the layout. If only 1 View has a layout weight then it will automatically fill the rest of the LinearLayout in that dimension.

=== Scrolling ===

In order to make the contents of the LinearLayout scrollable you have to do something a little strange: you make the layout a child of a [http://developer.android.com/reference/android/widget/ScrollView.html ScrollView]. Android will handle everything else, including creating and showing/hiding the scroll bar.

== RelativeLayout ==

The default layout but it's quite a bit more complicated than LinearLayout. Instead of everything being in one line, every View in this layout is arranged relative to some other view (or a side of the layout).

For example if you have two buttons you can arrange Button1 to align with the layout it's in using android:layout_alignParent* and align Button2 under Button1 using android:layout_below. See the XML Attributes section of [http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html RelativeLayout.LayoutParams]

This layout also can be used to create pretty much any imaginable layout, but I find it much harder to maintain.