There are two ways to set a style:
- To an individual View, by adding the
styleattribute to a View element in the XML for your layout. - Or, to an entire Activity or application, by adding the
android:themeattribute to the<activity>or<application>element in the Android manifest.
When you apply a style to a single
View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.
To apply a style definition as a theme, you must apply the style to an
Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont
style from the previous examples to an Activity, then all View elements
that support the text style properties will apply them. Any View that
does not support the properties will ignore them. If a View supports
only some of the properties, then it will apply only those properties.Apply a style to a View
Here's how to set a style for a View in the XML layout:
<TextView style="@style/CodeFont" android:text="@string/hello"/>
Now this TextView will be styled as defined by the style named
CodeFont. (See the sample above, in Defining Styles.)
Note: The
style attribute does not use the android: namespace prefix.Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the
AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:<applicationandroid:theme="@style/CustomTheme">
If you want a theme applied to just one Activity in your application, then add the
android:theme attribute to the <activity> tag instead.
Just
as Android provides other built-in resources, there are many
pre-defined themes that you can use, to avoid writing them yourself. For
example, you can use the
Dialog theme and make your Activity appear like a dialog box:<activityandroid:theme="@android:style/Theme.Dialog">
Or if you want the background to be transparent, use the Translucent theme:
<activityandroid:theme="@android:style/Theme.Translucent">
If you like a theme, but want to tweak it, just add the theme as the
parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:<colorname="custom_theme_color">#b0b0ff</color><stylename="CustomTheme"parent="android:Theme.Light"> <item name="android:windowBackground">@color/custom_theme_color</item> <item name="android:colorBackground">@color/custom_theme_color</item></style>
(Note that the color needs to supplied as a separate resource here because the
android:windowBackground attribute only supports a reference to another resource; unlike android:colorBackground, it can not be given a color literal.)
Now use
CustomTheme instead of Theme.Light inside the Android Manifest:<activityandroid:theme="@style/CustomTheme">
Select a theme based on platform version
Newer
versions of Android have additional themes available to applications,
and you might want to use these while running on those platforms while
still being compatible with older versions. You can accomplish this
through a custom theme that uses resource selection to switch between
different parent themes, based on the platform version.
For
example, here is the declaration for a custom theme which is simply the
standard platforms default light theme. It would go in an XML file
under
res/values (typically res/values/styles.xml):<stylename="LightThemeSelector"parent="android:Theme.Light"> ...</style>
To
have this theme use the newer holographic theme when the application is
running on Android 3.0 (API Level 11) or higher, you can place an
alternative declaration for the theme in an XML file in
res/values-v11, but make the parent theme the holographic theme:<stylename="LightThemeSelector"parent="android:Theme.Holo.Light"> ...</style>
Now
use this theme like you would any other, and your application will
automatically switch to the holographic theme if running on Android 3.0
or higher.
A list of the standard attributes that you can use in themes can be found at
R.styleable.Theme.
For
more information about providing alternative resources, such as themes
and layouts, based on the platform version or other device
configurations, see the Providing Resources document.
No comments:
Post a Comment