Application style and topic Applying Styles and Themes
When designing an application, you can use styles and themes to format various screen and UI elements in a unified manner.
L style is a set of one or more formatting attributes. You can use it as a unit in a single element of XML layout. For example, you can define a style to define the font size and color of the text, and then apply it to a specific instance of the view element.
L a topic is a set of one or more formatting attributes. You can use a topic as a unit for all activities of the application or an activity. For example, you can define a theme. It sets a specific color for the foreground and background of the window frame and panel, and defines the text size and color attributes for the menu, then, apply the topic to your application activity.
Style and topic are both resources. You can use some default styles and theme resources provided by Android, or customize your own theme and style resources.
To create a custom style and topic:
1. Create a file named styles. xml in the res/values directory of the application. Add a <resources> root node.
2. Add a <style> element to each style and topic with a globally unique name and an optional parent style attribute. Later, we can use this name to reference these styles. The parent style attribute indicates which style the current style belongs.
3. within the <style> element, define one or more <item> elements to describe the specific format. Each <item> indicates its style characteristics with a name attribute, the value of this style is defined in the element.
4. Then you can reference these custom resources in other XML resources, manifest or application code.
Styles
The following is an example of a declarative style:
[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "SpecialText" parent = "@ style/Text">
<Item name = "android: textSize"> 18sp </item>
<Item name = "android: textColor"> #008 </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "SpecialText" parent = "@ style/Text">
<Item name = "android: textSize"> 18sp </item>
<Item name = "android: textColor"> #008 </item>
</Style>
</Resources>
As shown above, you can use the <item> element to define a set of formatted values for your style. The name attribute in Item can be a string, a color represented by a hexadecimal number, or any reference to other resources.
Note the attributes of the parent class in the <style> element. This attribute allows you to define a resource. The current style can be inherited from this resource to the value. You can inherit this style from any resource containing this style. Generally, your resources should inherit the standard style resources of Android directly or indirectly. In this way, you only need to define the value you want to change.
The EditText element in this example demonstrates how to reference the style defined in an XML layout file:
[Java] <EditText id = "@ + id/text1"
Style = "@ style/SpecialText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello, World! "/>
<EditText id = "@ + id/text1"
Style = "@ style/SpecialText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello, World! "/>
The style of the EditText component is defined in the XML file above.
Themes
Just like the style, the topic is still stated in the <style> element and referenced in the same way. The difference is that the <application> and <activity> elements defined in Android Manifest Add the topic to the entire program or activity-the topic cannot be applied to individual views.
The following is an example of declaring a topic:
[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "CustomTheme">
<Item name = "android: windowNoTitle"> true </item>
<Item name = "windowFrame"> @ drawable/screen_frame </item>
<Item name = "windowBackground"> @ drawable/screen_background_white </item>
<Item name = "panelForegroundColor"> # FF000000 </item>
<Item name = "panelBackgroundColor"> # FFFFFFFF </item>
<Item name = "panelTextColor">? PanelForegroundColor </item>
<Item name = "panelTextSize"> 14 </item>
<Item name = "menuItemTextColor">? PanelTextColor </item>
<Item name = "menuItemTextSize">? PanelTextSize </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "CustomTheme">
<Item name = "android: windowNoTitle"> true </item>
<Item name = "windowFrame"> @ drawable/screen_frame </item>
<Item name = "windowBackground"> @ drawable/screen_background_white </item>
<Item name = "panelForegroundColor"> # FF000000 </item>
<Item name = "panelBackgroundColor"> # FFFFFFFF </item>
<Item name = "panelTextColor">? PanelForegroundColor </item>
<Item name = "panelTextSize"> 14 </item>
<Item name = "menuItemTextColor">? PanelTextColor </item>
<Item name = "menuItemTextSize">? PanelTextSize </item>
</Style>
</Resources>
Note that the @ symbol and? Symbol to reference resources. The address symbol @ indicates that the referenced resource is defined elsewhere (maybe in this project or in the Android framework ). Question mark? Indicates that the value of the referenced resource is in the currently loaded topic. This is done by referencing a specific <item> by name (for example, panelTextColor uses the same color allocated to panelForegroundColor ). This technique can only be used in XML resources.
Set the theme in the manifest topic Set the theme in the manifest
To set a topic for all activities of the application, you can open the list file AndroidManifest. xml, edit the <application> tag to include the android: theme attribute. The value is the name of a topic, as shown below:
[Java] <application android: theme = "@ style/CustomTheme">
<Application android: theme = "@ style/CustomTheme">
If you only want an Activity in your program to have this topic, you can modify the <activity> label.
Just like other built-in resources provided by Android, there are several topics that you can switch over without writing them by yourself. For example, you can use a dialog box topic to make your activity look like a dialog box. In manifest, an Android topic is referenced as follows:
[Java] <activity android: theme = "@ android: style/Theme. Dialog">
<Activity android: theme = "@ android: style/Theme. Dialog">
If you like a topic but want to make some minor adjustments, you only need to add it as a parent topic. For example, we will modify Theme. Dialog topic. To this end, create a Theme. Dialog style as the parent topic:
[Java] <style name = "CustomDialogTheme" parent = "@ android: style/Theme. Dialog">
<Style name = "CustomDialogTheme" parent = "@ android: style/Theme. Dialog">
In this way, we have inherited the theme of the Android dialog box, so we can adjust the theme as needed. In this way, for each item in the topic of this Dialog box that we want to change, we can redefine its value here and use CustomDialogTheme instead of Theme. Dialog in the Android Manifest file.
Set the topic Set the theme from the application in the program
If necessary, you can also program a topic to load an activity. To do this, use the setTheme () method. Note: In this case, you should set the topic before initializing the view in any context. For example, before calling the setContentView (View) or inflate (int, ViewGroup) method. This ensures that the system applies the current topic to all your user interface screens. Example:
[Html] protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
...
SetTheme (android. R. style. Theme_Light );
SetContentView (R. layout. linear_layout_3 );
}
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
...
SetTheme (android. R. style. Theme_Light );
SetContentView (R. layout. linear_layout_3 );
}
If you want to load the subject of the main interface in the program code, you must note that this topic cannot be applied to any system animation used to start this activity. These animations will occur before the application starts. In most cases, if you want to apply a topic to your main interface, defining it in XML is a better way.
For more information about custom styles and themes and how to reference them in applications, see Available Resource Types: Style and topic Available Resource Types: Style and Themesfile: /// D:/android-sdk-windows-1.5_r2/docs/guide/topics/resources/available-resources.html # stylesandthemes.
From sunset hut