After the XML declaration, Listing 2 declares a
LinearLayout element that specifies a layout (a view group that arranges contained views on an Android device's screen in some manner) for arranging contained widgets (including nested layouts) either horizontally or vertically across the screen.
<LinearLayout> tag specifies several attributes for controlling this linear layout. These attributes include the following:
- orientation identifies the linear layout as horizontal or vertical. Contained widgets are laid out horizontally or vertically, and the default orientation is horizontal.
"vertical"are the only legal values that can be assigned to this attribute.
- layout_width identifies the width of the layout. Legal values include
"fill_parent"(to be as wide as the parent) and
"wrap_content"(to be wide enough to enclose content). (Note that
fill_parentwas renamed to
match_parentin Android 2.2, but is still supported and widely used.)
- layout_height identifies the height of the layout. Legal values include
"fill_parent"(to be as tall as the parent) and
"wrap_content"(to be tall enough to enclose content).
- gravity identifies how the layout is positioned relative to the screen. For example,
"center"specifies that the layout should be centered horizontally and vertically on the screen.
- background identifies a background image, a gradient, or a solid color. For simplicity, I've hardcoded a hexadecimal color identifier to signify a solid white background (
#ffffff). (Colors would normally be stored in
colors.xmland referenced from this file.)
LinearLayout element encapsulates
Button elements. Each of these elements specifies an
id attribute, which identifies the element so that it can be referenced from code. The resource identifier (special syntax that begins with
@) assigned to this attribute begins with the
@+id prefix. For example,
@+id/android identifies the
ImageView element as
android; this element is referenced from code by specifying
These elements also specify
layout_height attributes for determining how their content is laid out. Each attribute is assigned
wrap_content so that the element will appear at its natural size.
ImageView specifies a
layout_marginBottom attribute to identify a space separator between itself and the button that follows vertically. The space is specified as
10 dips, or density-independent pixels. These are virtual pixels that apps can use to express layout dimensions/positions in a screen density-independent way.
About density-independent pixels
A density-independent pixel is equivalent to one physical pixel on a 160-dpi screen, the baseline density assumed by Android. At runtime, Android transparently handles any scaling of the required dip units, based on the actual density of the screen in use. Dip units are converted to screen pixels via equation pixels = dips * (density / 160). For example, on a 240-dpi screen, 1 dip equals 1.5 physical pixels. Google recommends using dip units to define your app's user interface to ensure proper display of the user interface on different device screens.
Sign up for CIO Asia eNewsletters.