Listing 39 Values resource defining reusable styles stylesxml

<?xml version="1.0" encoding= "utf-8"?>

<resources> O Use a <style> element

<item name= "'android: textSize">22sp</item> <item name= "android.-textColor">#ee7620</item> <item name= "'android: textStyle">bold</item> </style>

Use an

<item> element

<style name="label">

<item name= "'android: textSize">18sp</item> <item name="android:textColor">#ffffff</item> </style>

<style name= "edit_text">

<item name= "'android: textSize">16sp</item> <item name="android:textColor">#000000</item> </style>

. . . remainder of file omitted for brevity </resources>

The Android styles approach is a similar concept to using Cascading Style Sheets (CSS) with HTML. Styles are defined in styles.xml and then referenced from other resources or code. Each <style> element O has one or more <item> children that define a single setting ©. Styles are made up of the various View settings: sizes, colors, margins, and such. Styles are very helpful because they facilitate easy reuse and the ability to make changes in one place. Styles are applied in layout XML files by associating a style name with a particular View component, such as style="@style/ intro_blurb" (note that in this case style is not prefixed with the android: namespace; it is a custom local style and not one provided by the platform).

Styles can be taken one step further and used as themes. While a style refers to a set of attributes applied to a single View element, themes refer to a set of attributes being applied to an entire screen. Themes can be defined in exactly the same <style> and <item> structure as styles are. To apply a theme you simply associate a style with an entire Activity, such as: android:theme="@android:style/[stylename]".

Along with styles and themes, Android supports a specific XML structure for defining arrays as a resource as well. Arrays are placed in source in res/values/arrays.xml and are helpful for defining collections of constant values, such as the cuisines we used to pass to our ArrayAdapter back in listing 3.1. Listing 3.10 shows how these arrays are defined in XML.

0 0

Post a comment