When designing your program, you can use styles and themes to format various screen and UI elements.
* A style is a set of one or more formatting attribute values. You can use it as a unit for a single element in XML layout. For example, you can define a style to define the font size and color of the text, and then use it in a specific instance of the view element.
* Themes is a set of one or more formatting attribute values. You can use themes as a unit in all the activities of the application or in a specific activity of the application. For example, you can define a topic. It defines a set of colors for the foreground and background of the window frame and panel, and defines the size and color attributes of the text for the menu, you can apply this topic to all the activities in your program.
Similarities between style and topic :
Style and topic are both resources. You can use some default styles and theme resources provided by Android, or you can customize your own theme and style resources.
Differences between style and theme :
The difference is that the <Application> and <activity> elements defined in Android manifest Add the topic to the entire program or an activity,
However, a topic cannot be applied to a single view. [However, it seems that a single topic item can be applied in a separate view]
How to create custom styles and themes:
1. Create an XML file under the Res/values directory. The Style File Name Is style. XML, and the topic file name is themes. xml. Add a <resources> root node.
2. Add a globally unique name to <style> element for each style and topic, or add a parent class attribute. Later, we can use this name to apply the style, and the parent class attribute identifies the style inherited by the current style.
3. Declare one or more <items> within the <style> element. Each <item> defines a name attribute and the value of this style within the element.
4. You can apply resources defined in other XML files.
Style
The following is an example of declarative style:
<? 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 attribute of the name in item can be a string, the color represented by a hexadecimal number, or a 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 that contains 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:
Example 1:
<Edittext id = "@ + ID/text1"
Style = "@ style/specialtext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello, world! "/>
Now the edittext component shows the style defined in the XML file above.
Topic
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 an activity,
However A topic cannot be used in a single view. .
Below is an example of declaring the subject:
<? 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 we have used
@Symbol and? Symbol to apply resources. The @ symbol indicates that the resources of our application are defined in the front (or in the previous project or in the android framework ). Question mark? Indicates that the value of the referenced resource has been defined in the current topic. The name defined in <item> can be referenced (the color of paneltextcolor is the same as that defined in panelforegroundcolor ). This technique can only be used in XML resources.
Set topic in manifest
To use themes in all the activities during usage, you can open androidmanifest. edit the <Application> tag to include the Android: Theme attribute. The value is the name of a topic, as shown below:
<Application Android: theme = "@ style/customtheme">
If you only want an activity in your program to have this topic, you can modify the <activity> label.
Android provides several built-in resources. You can switch between several topics without writing them yourself.
For example, you can use a dialog box topic to make your activity look like a dialog box. Manifest is defined as follows:
<Activity Android: theme = "@ Android: style/theme. Dialog">
If you like a topic but want to make some slight changes, you only need to add it as a parent topic. For example, we modify theme. Dialog topic.
We will inherit theme. dialog to generate a new topic.
<Style name = "customdialogtheme" parent = "@ Android: style/theme. Dialog">
After inheriting theme. Dialog, we can adjust the theme as required. We can modify the value of each item element defined in theme. Dialog, and then use customdialogtheme instead of theme. Dialog in the android manifest file.
Set the topic in the program
If necessary, you can use settheme () in the activity to load a topic.
Note: If you do this, you should set the topic before initializing any view.
For example, before calling the setcontentview (View) and inflate (INT, viewgroup) methods.
This ensures that the system applies the current topic to all UI interfaces. Example:
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, note that the subject does not include any animations used by the system to start the activity. These animations will be displayed before the program starts.
In many cases, if you want to apply a topic to your main interface, it seems a better way to define it in XML.
Attribute Value of the referenced topic attribute
Other types of resources can reference the current topic. These attributes can only be style resources or XML attributes. In this way, you can customize your UI to maintain a consistent style with the current topic.
The following example shows how to use the system topic to set the text color in the layout.
Example 1:
<Edittext Android: Id = "@ + ID/edittext1" xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: textcolor = "? Android: textcolorsecondary "Android: text =" @ string/Hello "/>
Note: The method for referencing a topic attribute is similar to that for referencing a resource. The method is simply "?". Replace "@". When you use this tag, the resource name you provide must be located in the topic attribute, because the resource tool considers this resource attribute to be expected.
Android: textcolor = "? The accurate meaning of Android: textcolorsecondary is to set the value of the Android: textcolor attribute of the control to the value of the Android: textcolorsecondary attribute of the current topic. If a complete declaration is made, what is the form? Android: ATTR/Android: textdisabledcolor, for example, 2.
The naming syntax for referencing topic attributes is similar :? [Namespace:] type/Name,
The type is always ATTR, so the type can be omitted here. In addition, the namespace can be omitted, indicating the current topic; otherwise, it indicates the topic of the application corresponding to the namespace. Example 2: <edittext Android: Id = "@ + ID/edittext2" xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: textcolor = "? Android: ATTR/Android: textcolorsecondary "Android: text =" @ string/Hello "/> Example 3: <edittext Android: Id =" @ + ID/edittext3 "xmlns: android2 = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: textcolor = "? Android2: ATTR/Android: textcolorsecondary "Android: text =" @ string/Hello "/>