"Android XML" Android XML to Java Code series style (3)

Source: Internet
Author: User
Tags asop

Refactoring the code over the last one months feels much more comfortable, but the overall development progress has not changed. Let's talk today. Ways to convert style attributes to Java code

The first conclusion: the reference system style is not perfect, if we have to write Java code requirements, please try to avoid using the system style. Custom style is no problem.

What is a style?

(reference link) http://developer.android.com/guide/topics/resources/style-resource.html

"A style resource defines the format and look for A UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within The manifest file). "

A more common understanding is that the style is actually a set of attribute macros, which are specified in the control, and will be applied to the control when the XML is parsed with a set of properties of the style. Give me a chestnut:

Here is a style, defined in Res/value:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stylename= "CustomText"Parent= "@style/text">        <Itemname= "Android:textsize">20sp</Item>        <Itemname= "Android:textcolor">#008</Item>    </style></Resources>

Then we reference this style in a control in layout:

<? XML version= "1.0" encoding= "Utf-8" ?> < EditText     style = "@style/customtext"     android:layout_width= "Fill_parent"    android:layout_height= "Wrap_ Content "    android:text=" Hello, world! " />

In fact, he is equal to:

<? XML version= "1.0" encoding= "Utf-8" ?> < EditText     android:textsize = "20SP"     android:textcolor= "#008"    android:layout_width= "Fill_parent"      android:layout_height= "Wrap_content"    android:text= " Hello, world! " />

A bit of a macro to replace the taste, if the contact with the HTML students can easily think of css this thing, two want to do the basic is the same.

It should be noted that the property of the style has the lowest priority and is overwritten by the other properties of the control. This is taken for granted, otherwise the control of the attribute to defy (you have written me out, incredibly and I said I useless, to be style that the goods covered off?)

Let's go back to this style that we've defined, and we'll find that he has a property called parent. Here we use the object-oriented parent class to understand that the child style inherits the properties of the parent class. Similarly, the attributes of the parent class are lower than the child class attribute precedence. (You have written me out, incredibly and I said I useless, to be Dad's style cover off?)

Style is not what?

Style does not belong to the properties in Android namespace (not with Android:), there is no corresponding SetStyle method to use, and my understanding is that it belongs to the "mechanism" of XML preloading.

Custom Style Translations:

So for a custom style, translation is simple.

For a style, replace it with the attribute collection in the style, and continue to increase the parent class collection, and note the precedence (subclasses can override the parent class attribute).

Translation of System style:

There are two main problems in the translation of system style:

1. System style is built-in inside the system, different system style is not the same, especially in depth customization such as MIUI, FlyMe and other operating systems. If you just use some simple comparison standard style, such as ProgressBar, you can download the latest Android version of the source for Android SDK (path I don't remember) via SDK Manager, or download Asop directly ( Path Framework/base/core/res/res/values/styles.xml. (I used in the project is a 4.4 style file, 5.0 of the Sytle file is divided into several, and the name of the inside is a bit nonstandard)

Some students found that, such as ProgressBar inside the Style:android:attr/progressbarstylesmall, is indexed by ANDROID:ATTR. Android:attr is actually a property defined in theme, and the theme of the APK is defined in Androidmanifest.xml. In parsing XML, when encountering android:attr, he will find the corresponding item in the theme specified by the APK. The value of this item corresponds to the value of the style, such as:

Style=?android:attr/progressbarstylesmall corresponds to the most basic theme <  name= "Progressbarstylesmall">@android: Style/widget.progressbar.small </ Item >

Then you go to Styles.xml to find Widget.ProgressBar.Small. (Here's another thing to note is that the system style doesn't declare the parent property, but with the dot "." hierarchy, such as the Widget.ProgressBar.Small parent class here is Widget.progressbar.)

Another problem with this is that the various themes of a deeply customized operating system are subject to change, resulting in greater deviations.

2. The resources used in the system style, some of which we cannot obtain from the outside. Or an example of Widget.ProgressBar.Small:

    <stylename= "Widget.ProgressBar.Small">        <Itemname= "Android:indeterminatedrawable">@android:d Rawable/progress_small_white</Item>        <Itemname= "Android:minwidth">16dip</Item>        <Itemname= "Android:maxwidth">16dip</Item>        <Itemname= "Android:minheight">16dip</Item>        <Itemname= "Android:maxheight">16dip</Item>    </style>

He used the @android:drawable/progress_small_white system resources, but this resource is not public, we do not have the means to call this resource. Some students think of reflection, but reflection is not. Because all system resources generate a resource index file similar to R.java when Asop compiles, these files are used to invoke system resources. Not public is not declared, that is, you can not even know the ID, you can not get the corresponding resources. I have roughly read the parsing implementation of XML, found that the end of all into the native layer, in the native layer in C + + implementation, which let me look at code sigh.

However, the various styles of ProgressBar should be set through the API, which I have not confirmed. Refer to: [Android instance] Android set style property in Java code--Create ProgressBar object using code

For this internal resources to get experienced students welcome the exchange.

Project Code:

I implemented the build style element in my project, replacing the style element with Attritutes, and the relationship between the style elements, including the android:attr of theme:

Https://github.com/SickWorm/AndroidXMLToJava/blob/master/src/com/excelsecu/androidx2j/AX2JStyle.java

Copyright, reproduced Please specify the source:

Http://www.cnblogs.com/sickworm/p/4355396.html

Android XML Android XML to Java Code series style (3)

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.