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">#00 8</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:

<EditText id="@+id/text1"

style="@style/SpecialText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />

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

0 0

Post a comment