Android style is look like cascading stylesheets in web design—they allow you to separate the design from the content.
A style is a collection of properties that specify the look and format for a
View.
Note:
A View occupies a rectangular area on the screen.View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML Layout.
For example, by using a style, you can take this layout XML:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
And turn it into this:
<TextView style="@style/CodeFonts" android:text="@string/hello" />
All of the attributes related to style have been removed from the layout XML and put into a style definition called
CodeFont
, which is then applied with the style
attribute. You'll see the definition for this style in the following section.Defining Styles :
You can define the style to res/values directory of your project, The name of the XML file is arbitrary(Your choice to select name of xml or random), but it must use the
.xml
extension and be saved in the res/values/
folder.
Here's an example file with a single style:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CodeFonts" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> </resources>
Each child of the
<resources>
element is converted into an application resource object at compile-time, which can be referenced by the value in the <style>
element's name
attribute. This example style can be referenced from an XML layout as @style/CodeFonts
(as demonstrated in the introduction above).
The
parent
attribute in the <style>
element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.
you can inherit style from exisiting styles by using parent attribute.
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>