Android's style and theme translations

Source: Internet
Author: User

A Style is a collection of properties for the appearance and formatting of window and view. It can be used as height, spacing, font size, background color and other properties. A style is an XML resource file that is placed in a different folder from the layout file;

Style's design philosophy and Web same strain--that they all allow you to separate content and style.

For example, you can use the style to put the following XML file:

<textview    android:layout_width= "fill_parent"    android:layout_height= "Wrap_content"    android: Textcolor= "#00FF00"    android:typeface= "monospace"    android:text= "@string/hello"/>
Replace it with the following form:

<textview    style= "@style/codefont"    android:text= "@string/hello"/>
Look at the example above, all the styling properties are placed in a style named Codefont from the layout file, which is a style property. You'll see how this layout is defined in the next section.

You can think of theme as the entire activity or the style of the entire program, which has a larger coverage than the view in the example above. When a style is used as a theme, the entire activity or every view in the program will use the same style attribute if those attributes support it. For example, you can set the Codefont in an activity style so that all the fonts in the activity will be green "monospace".

Define Style
Generate an XML file under the res/values/path in your project and create a set of style inside it. You can take this XML file name arbitrarily, as long as it must be suffixed with. xml and saved inthe res/values/path is OK.
the XML file must have <resources> as the root node.
you add a unique name <style> element to the XML (name is required) for each style you want to create. Then add a <item> attribute with name and value in the style (name and value are also required). The value in <item> can be a string, a hexadecimal color value, a resource reference, or a property value that other style can use. Here is an example of a style with only a single style:
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <style name= "Codefont" parent= "@android: Style /textappearance.medium ">        <item name=" android:layout_width ">fill_parent</item>        <item Name= "Android:layout_height" >wrap_content</item>        <item name= "Android:textcolor" > #00FF00 </ item>        <item name= "Android:typeface" >monospace</item>    </style></resources>
Each element under <resources> is transferred to the program's resource class during compilation, and a value reference is generated in the name of <style>. The style in this example can be referenced by the layout file in @style/codefont. (as in the beginning of the article)
In <style> there is also a parent property, which is optional and you can specify another resource ID in this attribute as the style you want to inherit. Then you can replicate the inherited attributes.Note that the theme of the style as activity or the entire program is defined in the XML file as if it were a style of view. The style in the example above can be applied in a single view or as the subject of activity or the entire program. As for how to apply a style to a single view or as a subject of a program, it will be mentioned later.
Inherited
In the <style> element, the Parent property allows you to define a style to inherit other existing style, and then add or duplicate only those attributes that you need. The existing style includes the one you created and the system itself (with the style and theme, which comes with the style and theme from the Android platform). For example, you can inherit the default text appenrence style below the Android platform and then modify it:
<style name= "Greentext" parent= "@android: Style/textappearance" >        <item name= "Android:textcolor" ># 00ff00</item> </style>
If you want to inherit the style that you define yourself, you don't necessarily need to use the Parent property. Instead, you can prefix the style you want to inherit, separating the style you want to create with a little bit. For example, to create a codefont style that inherits from the above example, just sets the color red, you can create a new style like this:
<style name= "codefont.red" >        <item name= "Android:textcolor" > #FF0000 </item></style>
You see, in the example above, there is no parent property in the style, just put codefont as a prefix in the name attribute in front of red, which can also inherit all the attributes in Codefont. This style copies the TextColor property and turns the font color red. With this definition, you can refer to this new style in the @style/codefont.red way.You can also continue to inherit countless times like this, for example, you can extend Codefont, like this:
    <style name= "CodeFont.Red.Big" >        <item name= "android:textsize" >30sp</item>    </style >
The style of writing like the one above is inherited from Codefont and codefont.red, and then the Android:textsize attribute is added.
Note: This type of inheritance can only be used in your own defined style, and you cannot apply it to the style that comes with the Android platform.  For an Android platform like Textappearance, you can only use the parent property to inherit the style that comes with it.
Style properties
Now that you know how to define a style, then you need to know what style attributes are available in the <item> element. You may be familiar with some properties, such as Layout_width and TextColor. Of course, there are a lot of properties you can use.
The best place to find these attributes is the reference to the corresponding class of view, which lists all the attributes supported in the XML. For example, all TextView properties that can be used in a style are listed in the TextView XML attributes table. There are also tables with some attribute values listed, such as this oneandroid:inputtype Table, you can use the attribute value inside as a property of <EditText>, as in the following example:
<edittext    android:inputtype= "number" ...    />
You can also create a style as an attribute of EditText instead of the one that follows:
<style name= "Numbers" >  <item name= "Android:inputtype" >number</item>  ...</style>
This style can be used for your edittext layout file:
<edittext    style= "@style/numbers" ...    />
This simple example may make you feel more troublesome with a style, but not necessarily, when you are in a real project, write a lot of properties that need to be reused in a style, and let all the attributes that are used in it, then you will feel a lot of trouble saving.
For a reference to all of the style's available properties, you can look at the r.attr link. Note that not all view classes accept all of the same style attributes, so you might want to create as many properties as you want in the style that you need to apply this style to. However, if a view view has a style applied, but the view does not support all the attributes in the style, then the view will use the attributes it uses and ignore those that it does not have.
There are also style attributes that are only supported for use in the theme theme and not for a single view. These style properties are only supported for the entire window and do not support a single view. For example, some style attributes can be used as themes to hide the program's title, status bar, or to change the background color of the entire window. These properties do not belong to any one of the views view classes. If you want to know which style attributes are unique to the window, you can refer to the r.attr in the beginning of the link. For example, the Windownotitle and Windowbackground properties are only useful when the style is applied to the activity or the entire program. The next section is about how style is applied to the activity or program as a theme.
Note: Do not forget that in each <item> element, you need to put Android: This namespace in front, for example: <item name= "Android:inputtype" >.
Setting style and theme on the UI
There are two ways to set style:1: For a separate view View,style set the properties in the view's layout file.2: For the entire activity or the entire program, as long as the Android:theme attribute in the Androidmanfest is added to the <activity> tag or <application> on the line.
When you set the Style property only in a single view, the property value inside the style only works on the view. If you set a style in ViewGroup, it will not have any effect on the sub-view inside the ViewGroup, it only works on the view you set. If you want to set a style to make the sub-view below the layout file work, you can set the style as a theme.
To think of style as theme, you must set it in the activity or appliaction in the androidmainfest. If you do this, the attributes in the style will work in every view of the activity or the entire program, if those attributes support it. For example, if you set the Codefont style in the previous example to an activity, the properties in the text style will take effect in all view views below it. It is automatically ignored for properties that it does not support, and view only works on properties that it can support.
Set style in view
Here is a style set in the View layout:
<textview    style= "@style/codefont"    android:text= "@string/hello"/>
Now the properties in style Codefont will be set into TextView. Note: When setting the Style property, do not add Android: this namespace.
Set a theme in activity or application
If you want to have the same theme for all the activity in a program, you can open the Androidmainfest.xml file, add the Android:theme attribute to the <application> tag, and the name of the style you want to refer to. For example:
<application android:theme= "@style/customtheme" >
If you want to set an activity theme, just add the Android:theme attribute to the <activity> tag.In fact, Android itself also provides a lot of resource files for you to call, avoiding all the attributes that you create yourself. For example, you can use the dialog theme to make your activity look like a dialog box:
<activity android:theme= "@android: Style/theme.dialog" >
Or if you want to make its background translucent, you just use the translucent theme:
<activity android:theme= "@android: Style/theme.translucent" >
If you like a theme, but you want to tweak some properties, you can create a new theme and inherit it with the parent property. For example, you can add your own colors above the ordinary light theme:
<color name= "Custom_theme_color" > #b0b0ff </color><style name= "Customtheme" parent= "Android: Theme.light ">    <item name=" Android:windowbackground "> @color/custom_theme_color</item>    <item name= "Android:colorbackground" > @color/custom_theme_color</item></style>
(Note that the Android:windowbackground attribute value in the above example only supports a color reference that has already been defined, unlike the Android:colorbackground attribute value, which can be used directly with the color values)You can now replace Theme.light with your custom theme in Androidmainfest:
<activity android:theme= "@style/customtheme" >

Select a platform-based version of the theme
For new versions of Android, there are additional themes that you might want to apply to older versions of the program. You can customize a theme in different versions of the resource files directory to inherit the old and new versions of the theme.
For example, the following example declares a custom theme on an older system platform whose directory is under Res/values (usually Res/values/styles.xml):
<style name= "Lightthemeselector" parent= "Android:Theme.Light" >    ...</style>
In the new version of Android 3.0 (API11) above, use the above theme to inherit from the theme holographic, you can declare this topic under other Resources directory, as long as its parent property is holographic system:
<style name= "Lightthemeselector" parent= "Android:Theme.Holo.Light" >    ...</style>
Now you can use any theme like this example, your program will automatically convert to the holographic series theme as long as your program runs on Android 3.0 or later.
You can find out what properties are available in the standard theme in R.styleable.theme.
If you want to know the different versions of resources, such as themes and layouts, what are the differences between different platforms and devices, you can look at providing resource this document.
Using the platform's style and theme
Android platform provides a collection of "d98" styles and themes in the original:a d98 large collection of styles and themes), you can use it in your program. You can find all the styles you can quote in the R.style class. When using the style in the link, you need to remove the underscore. For example, you can use the Theme_notitlebar theme in the "@android: Style/theme.notitlebar" way.
However, the above R.style document is not a good document, it does not describe the style in detail, so you are best to look at these styles and themes of the source, it will give you a better understanding. For Android styles and themes, there are two source documents available for reference:
1:Android Styles (styles.xml)2:android Themes (Theme.xml)
These two documents can be very helpful to you through examples. For example, in the source code of the Android theme, you will find the <style name= "Theme.dialog" > statement, in which you will see what properties the Android framework defines for the Dialog style.
If you want to know more about styles and themes syntax in XML, see style resource this document.
For more information about which properties in a style or theme can be defined (for example, "Windowbackground" or "textappearance"), see R.attr or the corresponding view class document. 

Style and theme translations for Android

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.