Working with Styles Android

A style is a group of common View attribute values. You can apply the style to individual View controls. Styles can include such settings as the font to draw with or the color of text. The specific attributes depend on the View drawn. In essence, though, each style attribute can change the look and feel of the particular object drawn.

In the previous examples of this chapter, you have seen how XML layout resource files can contain many references to attributes that control the look of TextView objects. You can use a style to define your application’s standard TextView attributes once and then reference to the style either in an XML layout file or programmatically from within Java.we see how you can use one style to indicate mandatory form fields and another to indicate optional fields. Styles are typically defined within the resource file res/values/styles.xml.The XML file consists of a resources tag with any number of style tags, which contain an item tag for each attribute and its value that is applied with the style.

The following is an example with two different styles:

When applied, this style sets the padding to two pixels and the textSize to eight pixels. The following is an example of how it is applied to a TextView from within a layout resource file:

Styles support inheritance; therefore, styles can also reference another style as a parent. This way, they pick up the attributes of the parent style. The following is an example of how you might use this:

Here you find two common attributes in a single style and a reference to them from the other two styles that have different attributes. You can reference any style as a parent style; however, you can set only one style as the style attribute of a View. Applying the padded_italics style that is already defined makes the text 14 pixels in size, italic, red, and padded.The following is an example of applying this style:

As you can see from this example, applying a style with a parent is no different than applying a regular style. In fact, a regular style can be used for applying to Views and used as a parent in a different style.

Here the padded_xlarge style is set as the parent for the green_glow style. All six attributes are then applied to any view that this style is set to.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics