Java Reference
In-Depth Information
zontal.
"horizontal"
and
"vertical"
aretheonlylegalvaluesthatcan
be assigned to this attribute.
•
layout_width
identifies the width of the layout. Legal values include
"fill_parent"
(be as wide as the parent) and
"wrap_content"
(be
wide enough to enclose content).
fill_parent
was renamed to
match_parent
in Android 2.2, but is still supported and widely used.
•
layout_height
identifies the height of the layout. Legal values include
"fill_parent"
(be as tall as the parent) and
"wrap_content"
(be tall
enough to enclose content).
•
gravity
identifieshowthelayoutispositionedrelativetothescreen.Forex-
ample,
"center"
specifiesthatthelayoutshouldbecenteredhorizontallyand
vertically on the screen.
•
background
identifies abackgroundimage, agradient, orasolidcolor.For
simplicity,I'vehardcodedahexadecimalcoloridentifiertosignifyasolidwhite
background (
#ffffff
).
The
LinearLayout
element encapsulates
ImageView
and
Button
elements.
Each of these elements specifies an
id
attribute that 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/duke
identifies the
ImageView
element as
duke
; this element is referenced from code by
specifying
R.id.duke
.
Theseelementsalsospecify
layout_width
and
layout_height
attributesfor
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
separatorbetweenitselfandthebuttonthatfollowsvertically.Thespaceisspecifiedas
10dips,or
density-independent pixels
(virtualpixelsthatappscanusetoexpresslayout
dimensions/positions in a screen density-independent way).
Note
Adensity-independent pixel isequivalent toonephysical pixel ona160-dpi
screen, the baseline density assumed by Android. At run time, Android transparently
handlesanyscalingoftherequireddipunits,basedontheactualdensityofthescreen
inuse.Dipunitsareconvertedtoscreenpixelsviaequationpixels=dips*(density/
160). For example, on a 240-dpi screen, 1 dip equals 1.5 physical pixels. Google re-
commendsusingdipunitstodefineyourapp'suserinterfacetoensureproperdisplay
of the UI on different screens.