Android fragmented knowledge of style and Theme

Source: Internet
Author: User

The various styles that we define in the app exist in the Android style resource file, which are all elements that begin with a kind, and contain a collection of many properties. But we are generally divided into style and theme, what is the difference between them?

One, Style or themewhat

Theme is a special style, and we usually think of style as being applied to a view or window, while theme is used in activity or application. The style is introduced in the layout file via [email protected]/[style name], notice there is no namespace prefix, theme by Android:[email Protected]/[style name].
There is a very important feature to distinguish between using style or theme, and the scope of the style element through [email Protected]/[style name] is only the view itself, not its child view; The style element that is mapped through Android:theme will scope its child view.
In fact, this distinction is well understood, style is the meaning of "style", it refers to the style of individual elements, and theme is "theme", the theme is the whole concept, so it will affect elements and elements of the child elements.

Why

Why do I need a style? The purpose of style is two (personal summary, not necessarily comprehensive):

    • Extracts public property items, reducing the amount of code.
    • Maintain the overall appearance of the interface, make the application interface style consistency.
Ii. Definition of Style

The format of the theme and style is the same, except that they have different objects and scopes. So here's how to define a style

where

The style must be defined in the styles file under the value directory, which is in XML format, the root element is resource, and within resource you can define your own style. The style is defined in the following location:

<Resources>    <stylename= "Apptheme"Parent= "Theme.AppCompat.Light">       <Itemname= "Colorprimary">@color/colorprimary</Item>       <Itemname= "Colorprimarydark">@color/colorprimarydark</Item>       <Itemname= "Coloraccent">@color/coloraccent</Item>   </style>    <stylename= "Apptheme.noactionbar">       <Itemname= "Windowactionbar">False</Item>       <Itemname= "Windownotitle">True</Item>   </style> </Resources>

How

Name must be written in the definition of style, which indicates the name of the style and the unique identity tag when using the style: R.style. [Name]
Next is the optional parent field, which allows Java-like infrastructure to inherit all of the properties of the parent's style, overwriting the original property or adding a new property.
The amount of code is reduced to a large extent. There is another way to represent an inheritance:

<!--inherit your own style of writing -<stylename= "Apptheme.noactionbar">...</style><!--General Inheritance Method -<stylename= "Apptheme"Parent= "Theme.AppCompat.Light">...</style>

As shown above, the first type of inheritance applies only to the inheritance of multiple styles in the same file, and the latter can inherit from the Android predefined style and the same file style.

Use

A well-defined style can be used in two ways.

    • Introducing an activity or application or other required element by android:theme= "@style/name";
    • Introduced by style= "@style/[name]" in the layout file.

The last style can also be used to fit different devices with a suffix file, which is not mentioned.

Android fragmented knowledge of style and Theme

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.