Here's an example declaration of a style:

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

<style name="SpecialText" parent="@style/Text"> <item name="android:textSize">18sp</item> <item name="android:textColor">#008</item> </style> </resources>

As shown, you can use <item> elements to set specific formatting values for the style. The name attribute in the item can refer to a standard string, a hex color value, or a reference to any other resource type.

Notice the parent attribute in the <style> element. This attribute lets you specify a resource from which the current style will inherit values. The style can inherit from any type of resource that contains the style(s) you want. In general, your styles should always inherit (directly or indirectly) from a standard Android style resource. This way, you only have to define the values that you want to change.

Here's how you would reference the custom style from an XML layout, in this case, for an EditText element:

• A style is a set of one or more formatting attributes that you can apply as a unit to single elements in your layout XML file(s). For example, you could define a style that specifies a certain text size and color, then apply it to instances of a certain type of View element.

• A theme is a set of one or more formatting attributes that you can apply as a unit to all activities in an application or just a single activity. For example, you could define a theme that sets specific colors for the window frame and the panel foreground and background, and sets text sizes and colors for menus, then apply it to the activities of your application.

Styles and themes are resources. Android provides some default style and theme resources that you can use, or you can declare your own custom style and theme resources.

<EditText id="@+id/text1"

style="@style/SpecialText" android:layout width="fill parent" android:layout height="wrap content" android:text="Hello, World!" />

Just like styles, themes are also declared in XML <style> elements, and are referenced in the same manner. The difference is that you add a theme to an entire application or activity, via the <application> and <activity> elements in the Android Manifest — themes cannot be applied to individual Views.

Here's an example declaration of a theme:

<?xml version="

1.0" encoding="utf-8"?>


<style name="


<item name=


<item name=

"windowFrame">@drawable/screen frame</item>

<item name=

"windowBackground">@drawable/screen background white</item>

<item name=


<item name=


<item name=


<item name=


<item name=


<item name=




Notice the use of the at-symbol (@) and the question-mark (?) to reference resources. The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from this project or from the Android framework). The question-mark indicates that we're referencing a resource value in the currently loaded theme. This is done by referring to a specific <item> by its name value. (E.g., panelTextColor uses the same color assigned to panelForegroundColor, defined beforehand.) This technique can be used only in XML resources.

Now this EditText widget will be styled as defined by the XML example above.


0 0

Post a comment