Android style and themes)

Source: Internet
Author: User

Android style and themes)

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 attributes. You can use it as a unit to deploy a single element in XML. 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.

A topic is a set of one or more formatting attributes. You can use a topic as a unit in all the activities of the application or an activity in 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.

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.
How to create custom styles and themes:
1. Create a file named style. XML in the Res/values directory. 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:
[Java] view plaincopyprint? <? 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 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 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] view plaincopyprint? <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! "/>

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 applied to a single view.

Below is an example of declaring the subject:
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 applied to a single view.

Below is an example of declaring a topic: [Java] view plaincopyprint? <? 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 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:

Note that the @ 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:

[Java] view plaincopyprint? 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, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.