• Most
obvious
usage
award android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="@string/hello" />
• Stores
a
scaling
value • Good
for
layout_width,
layout_height,
textSize,
etc. android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="@string/hello" android:textSize="@dimen/my_text_size" />
• Can
be
used
for
true/false
a]ributes • Useful
for
determining
configura:on if (res.getBoolean(R.bool.is_landscape)) { tv.setText(R.string.landscape_detected); }
Fragment Tag android:name="com.app.MyPrefFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:tag="@string/tag" />
When to Use Styles On Views • When
repea:ng
the
same
View
a]ributes
along
similar
Views
(e.g.,
label
TextViews) • NOT
for
every
single
View • NOT
when
two
Views
happen
to
have
some
a]ributes
in
common
Styles and Resource Qualifiers • A]ributes
inside
of
styles
fall
under
the
resource
qualifier
system • Styles
themselves
fall
under
the
resource
qualifier
system • Use
resource
qualifiers
to
swap
out
the
parent
style!
Example: Holo Theme • Problem:
Holo
only
introduced
in
v11 • Solu*on:
Use
resource
qualifiers
+
style
paren:ng
to
switch
which
parent
Theme
to
use
in
different
contexts
Layout Aliases • Problem:
handling
both
old
and
new
screen
size
qualifiers • Old
style: –/layout-‐small/, –/layout-‐large/ • New
style: –/layout-‐sw800dp/ –/layout-‐h500dp/
• Create
two
different
Ac:vity
entries
in
AndroidManifest.xml
that
point
to
the
same
Ac:vity. • Uses: –Alterna:ve
entrances
into
one
Ac:vity –Backwards
compa:bility
Activity Inheritance • Suppose
you
want
to
apply
a
behavior
to
all
your
Ac:vi:es • Problem:
you
have
a
mixture
of
Ac:vity,
ListAc:vity,
MapAc:vity,
etc • Solu:on:
Composi:on
over
inheritance
Activity Inheritance 1.Create
an
Ac:vity
Helper
that
implements
func:onality. public class TrackingHelper { public void onResume() { // Tracking code } }
Activity Inheritance 2.Add
the
helper
to
your
Ac:vity public class MyActivity extends Activity { private TrackingHelper mTrackingHelper = new TrackingHelper(); public void onResume() { super.onResume(); mTrackingHelper.onResume(); } }
Libraries • Leverage
these
to
reuse
common
components
between
your
applica:ons • Problem:
what
if
you
want
to
reskin
a
component
from
a
library? • Answer:
A]ributes
and
themes
Styling Library Activities 2.Link
to
those
a]ributes
in
the
library's
XML. android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="?attr/ myBackground" />
Styling Library Activities 3.In
the
applica:on
using
the
library,
create
a
theme
which
defines
values
for
those
a]ributes. <br/><item name="myBackground">#000</item><br/>
Styling Library Activities 4.Apply
that
theme
to
the
Applica:on
(or
Ac:vity) android:icon="@drawable/icon" android:label="My App" android:theme="@style/LibraryTheme">
Warning • If
you
try
to
access
an
a]ribute
that
is
not
defined,
you
get
a
very
ugly
(and
difficult
to
debug)
explosion • Create
a
default
theme
that
applica:ons
using
your
library
can
extend
(to
avoid
explosions)