Theme and style applications in Android

Source: Internet
Author: User

Theme and style were first introduced because I wanted to change the style of the title bar. Fortunately, I found a very good article: solving the problem that the custom title bar of Android is full. This article explains in detail the problem solving process, which has benefited me a lot.

What are theme and style?

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 in the application or a specific 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.

So 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.

To inherit other styles, you can use the parent attribute to specify them. Reference other style sheets. If it is an application system, you need to use Android: style/(in fact, you only need to write it for Android, but it is best to write it for good looks, for custom styles, use style/headers. The same applies to inheritance. When "@" or "? ", @ 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.

For example, the following style example:

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

So how to use it?

<Edittext id = "@ + ID/text1"
Style = "@ style/specialtext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello, world! "/>

What if the topic is used?

<Activity Android: Name = ". About"
Android: Label = "@ string/about_title"
Android: theme = "@ Android: style/theme. Dialog">
</Activity>

How to Set a topic in a 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.

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.