Style and themes in Android Application Development)

Source: Internet
Author: User
ArticleDirectory
    • Set topic in manifest
    • Set the topic in the program

When you design yourProgramYou 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 a custom style and topic: 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. ------------------------------- The following is an example of a 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 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:

 
<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. The topic is like a style. The topic is still stated in the

 
    


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. By referencing the name defined in <item> (PaneltextcolorUsed color andThe same is defined in panelforegroundcolor.). This technique can only be used in XML resources.In manifest, you can set the topic to use the topic for all the activities in the usage.Edit the ndroidmanifest. xml file<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 useCustomdialogtheme insteadTheme. dialog.Set the topic in the programIf 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 plan to load the topic of the main interface in the Code program, note that the subject cannot contain any animations used to start the activity by the system. These animations will be displayed before the activity is started. In many cases, if you want to apply a topic to your main interface, it seems a better way to define it in XML. Http://www.haiyangjy.com Translation: http://developer.android.com/guide/topics/ui/themes.html

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.