Android-Styles and Themes [From API Guide]
This blog was opened 5 months ago and it has 57 posts now,but the poor thing is by now no one has commented on any articles by now..How miserable and pathetic...Even if I've been writing some bullshit you should at least give me some response...Never mind, I was originally meant to writer to remind myself.
And sometimes I feel a little reluctant to read English than Chinese, even when I carefully read them I feel they are illustrated so clearly even more explicit than many Chinese vague articles.When I learn something from google's developer.android.com , I want to note them here.But I think why, firstly, why should I note them when I can visit them any time on google's website; secondly ,why use Chinese?
There's a proverb in China called "A good memory is not as good as a broken pen point" ,meaning that one would remember something better when he or she write it down.Ok now I decide to write things down when I learn something from android developers.
-------------------------
Styles and Themes:
Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.
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/CodeFont" android:text="@string/hello" />
All of the attributes related to style have been removed from the layout XML and put into a style definition calledCodeFont
, which is then applied with the style
attribute.
A theme is a style applied to an entire Activity
or application, rather than an individual View
. When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont
style as a theme for an Activity and then all text inside that Activity will have green monospace font.
Define Styles
1.To create a set of styles, save an XML file in the res/values/
directory of your project. The name of the XML file is arbitrary, but it must use the .xml
extension and be saved in the res/values/
folder.(I thought it could be saved in any folder,is it just a recommendation?)
2.The root node of the XML file must be <resources>
.
3.For each style you want to create, add a <style>
element to the file with a name
that uniquely identifies the style (this attribute is required). Then add an <item>
element for each property of that style, with a name
that declares the style property and a value to go with it (this attribute is required). The value for the <item>
can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CodeFont" 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>
-----it's Tomb-sweeping Day , to be continued-----
Inheritance
You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform.
For example, you can inherit the Android platform's default text appearance and then modify it:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>
kind of like the "override" concept in Java huh..