Android style and themes

Source: Internet
Author: User

Android XML style and topic file writing involve the entireProgramOne of the factors that make the interface look beautiful. Better Application style and themes can achieve a beautiful and unified interface, which is like CSS in Web development.

Styles and themes are both resources for storage.In Res/ValuesFolder.

What is style and theme?
    • Style: a set of one or more formatting attributes. We can use it as a unit to use it in a single element of XML layout. For example, we 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.
    • Theme: a set of one or more formatting attributes. We can use theme as a unit in all the activities of the application or an activity in the application. For example, we can define a theme, which 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, this theme can be applied to all the activities in your program.
XML file structure of style and theme

For each styles and themes<Style>Add a globally unique name to the element, 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. In the <style> element, declare one or more<Item>,Each <item> defines a name attribute and defines the value of this style within the element.

Style

1. Create a file named style. XML in the Res/values directory.

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 the style provided by the SDK: (the program provided by the SDK has a style/Text error that cannot be found. I do not know how to solve it yet)

<? 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>

The above style can be used in a single view, such:

<Edittext id = "@ + ID/text1 ″
Style = "@ style/mytext"
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.

Write a simple style:

 
<? XML version ="1.0"Encoding ="UTF-8"?> <Resources> <style name ="Specialtext"> <Item name ="Android: textsize"> 18sp </item> <item name ="Android: textcolor"> # Ec9237 </item> </style> <style name ="Specialtext2"> <Item name ="Android: textsize"> 26sp </item> <item name ="Android: textcolor"># Ff7f7c </item> <item name ="Android: fromalpha"> 0.0 </item> <item name ="Android: toalpha"> 0.0 </item> </style> </resources>

Application Compilation:

<Textview style ="@ Style/specialtext2"Android: layout_width ="Fill_parent"Android: layout_height ="Wrap_content"Android: text ="@ String/Hello"/> <Edittext Android: text ="@ + ID/edittext01"Style ="@ Style/specialtext"Android: Id ="@ + ID/edittext01"Android: layout_width ="Wrap_content"Android: layout_height ="Wrap_content"> </Edittext>

The final implementation interface is:

Of course, Android also has many predefined themes:

Topic

Like style, theme 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.

The following is an example of the topic in the SDK: (the program provided by the SDK has errors such as error: no resource found that matches the given name: panelforegroundcolor. Currently, I still don't know how to solve these errors)

<? 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.By referencing the name defined in <item> (PaneltextcolorUsed color andThe same is defined in panelforegroundcolor.). This technique can only be used in XML resources.

How to use a topic in a program:

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Settheme (Android. R. style. theme_light );
Setcontentview (R. layout. linear_layout_3 );
}

Apply the topic in mainfest. xml:
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.

A simple theme:

 
   1.0 " encoding = " UTF-8 "?> 
   
    
   

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.

Other predefined topics are listed below:

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.