style
A style is a collection of properties that can specify the look and format of a View or window. Styles can specify height, padding, font color, size, background color, and many other properties. Styles are defined in a different XML resource than the XML file that specifies the layout.
The theme
A theme is a style that is applied to an entire Activity or application rather than a single View. When styles are applied as themes, each view in the Activity or application applies each style attribute it supports.
Definition of style
To create a set of styles, save an XML file in your project’s RES /values/ directory. You can name the XML file any way you want, but it must use the.xml extension and must be saved in the RES /values/ folder.
The root node of the XML file must be < Resources >.
For each style you want to create, add a
<? xml version="1.0" encoding="utf-8"? > <resources> <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Copy the code
Each child of the
element is converted at compile time to an application resource object, which can be referenced by the value in the name attribute of the
The parent attribute in the
Note that in
XML
Define what you want to use asActivity
orApplication of theme
The style is exactly the same as defining the view style. Styles such as those defined above can be applied as a single view or as a wholeActivity
或Application theme
Apply it.
Style inheritance
Specify the style that should be the source of attributes your style inherits through the parent attribute in the
For example, you can inherit the default text look of the Android platform and then modify it:
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
Copy the code
If you want to inherit properties from your own style, you don’t have to use the parent property. Instead, you just prefix the name of the style you want to inherit to the name of the new style, separated by a period.
For example, to create a new style that inherits the CodeFont style defined above, but with the color set to red, you could create the new style as follows:
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
Copy the code
Note that there is no parent attribute in the
You can continue this inheritance as many times as you want by using the period link name. For example, you can expand codefont.red by:
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
Copy the code
This code inherits from both the CodeFont and codefont. Red styles, and then adds the Android :textSize attribute.
Note: This inheritance by linking names only works for styles defined by your own resources. You can’t inherit Android’s built-in styles this way. To reference a built-in style (such as TextAppearance), you must use the parent attribute.
Apply styles and themes to the UI
There are two ways to set styles:
- If you are styling a single View, add the View element in the layout XML
style
Properties.<TextView style="@style/CodeFont" android:text="@string/hello" /> Copy the code
- Or, if you are styling an entire Activity or application, add the Android: Theme attribute to the or element in the Android manifest.
<application android:theme="@style/CustomTheme"> <activity android:theme="@android:style/Theme.Dialog"> Copy the code
When you apply a style to a single View in a layout, the properties defined by that style apply only to that View. If you style a ViewGroup, the child View elements will not inherit the style attributes and only the elements you style directly will have their attributes applied. However, you can apply styles to all View elements by applying them as themes.
To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. If you do this, every View in your Activity or application will apply every attribute it supports. For example, if you apply the CodeFont style from the previous example to an Activity, all View elements that support these text-style attributes will also apply them. Any View that does not support these properties will ignore them. If a View supports only some properties, only those properties will be applied.
Select themes based on platform version
Newer versions of Android provide more themes for applications, and you may want to use these new themes when running on these platforms while still being compatible with older versions. You can do this by customizing themes that switch between parent themes based on platform versions using resource options.
For example, the following declaration corresponds to a custom theme that is the standard platform default bright theme. It is in an XML file (usually res/values/styles.xml) under RES /values:
<style name="LightThemeSelector" parent="android:Theme.Light">... </style>Copy the code
To make the theme use an updated holographic theme when running on Android 3.0 (API level 11) or higher, you can add an alternative theme declaration in the XML file under RES /values-v11, but set the parent theme to a holographic theme:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">... </style>Copy the code
Now use this theme as you would any other theme, and your app will automatically switch to the holographic theme when it runs on Android 3.0 or later.