[Android XML] Android XML to Java Code series style (3), androidxml

Source: Internet
Author: User

[Android XML] Android XML to Java Code series style (3), androidxml

The Code has been restructured in the last month, and it seems much more comfortable, but the overall development progress has not changed .. today we will talk about how to convert the style attribute into Java code.

 

Conclusion: Referencing System styles cannot be implemented perfectly. If you need to write Java code, try to avoid using system styles. Custom styles are fine.

 

What is 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 individualView(From within a layout file) or to an entireActivityOr application (from within the manifest file )."

In a more general sense, a style is actually a set of attribute macros. Specifying this style in the control will apply a set of attributes of the style to the control during xml parsing. example:

There is a style defined in res/value:

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="CustomText" parent="@style/Text">        <item name="android:textSize">20sp</item>        <item name="android:textColor">#008</item>    </style></resources>

Then, a control in layout references this style:

<?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, it is equal:

<?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!" />

There is a taste of macro replacement. If you have been familiar with html, you can easily think of CSS. The two things you want to do are basically the same.

It must be specified that the style attribute has the lowest priority and will be overwritten by other identical attributes of the control. this is a matter of course, or the attribute in the control will not be satisfied (you have written me out, and told me that I am useless. Will the style goods be overwritten ?!)

 

Let's go back to the defined style and we will find that it has a property called parent. here we use the object-oriented parent class to understand that the child style inherits the attributes of the parent class. similarly, the parent class has a lower priority than the Child class. (You wrote me out and told me that I was useless. Should I be overwritten by my dad's style ?!)

 

What is style?

Style does not belong to the attributes in the android Namespace (not starting with android:), and there is no corresponding setStyle method available. In my understanding, it is a "mechanism" of xml pre-loading ".

 

Custom style translation:

The translation of custom styles is simple.

For a style, replace it with the attribute set in the style, and add the parent class set up (parent class), and pay attention to the priority (Child class can overwrite the parent class attribute ).

 

System style translation:

There are two main problems in system style translation:

1. system styles are built in the system. Different system styles are not the same, especially for highly customized operating systems such as MIUI and Flyme. if you only use some simple standard styles, such as progressbar, you can download the latest Android version of the API through SDK Manager, then, find the standard system style in the path platforms \ android-20 \ data \ res \ values, or directly download ASOP (path framework/base/core/res/values/styles. xml. (I used 4.4 style files in my project. 5.0 of sytle files are divided into multiple files, and the names in the files are somewhat nonstandard)

Some colleagues found that, for example, the style: android: attr/progressBarStyleSmall in progressbar is indexed by android: attr. android: attr is actually defined in theme. The theme of apk is in AndroidManifest. defined in xml. when android: attr is encountered during xml parsing, it will find the corresponding item in the theme specified by apk. the value corresponding to this item is the value of the style, for example:

Style =? Android: attr/progressBarStyleSmall corresponds to <item name = "progressBarStyleSmall"> @ android: style/Widget. ProgressBar. Small </item>

Then you go to styles. search for widgets in xml. progressBar. small. (note that the system style does not declare the parent attribute ". "level, such as the Widget. progressBar. the Small parent class is a Widget. progressBar)

This brings about another problem. The various themes of the deeply customized operating system will also be changed, which leads to a greater deviation.

 

2. The resources used by the system style, some of which cannot be obtained from the outside, or the example of Widget. ProgressBar. Small:

    <style name="Widget.ProgressBar.Small">        <item name="android:indeterminateDrawable">@android:drawable/progress_small_white</item>        <item name="android:minWidth">16dip</item>        <item name="android:maxWidth">16dip</item>        <item name="android:minHeight">16dip</item>        <item name="android:maxHeight">16dip</item>    </style>

It uses the system resource @ android: drawable/progress_small_white, but this resource is not public. We cannot call this resource during implementation. some students think of reflection, but reflection does not work. because all system resources will generate a file similar to R. resource index files such as java are used to call system resources using the system resource id. instead of public, it will not be declared, that is, you cannot even know the id, and you will not be able to get the corresponding resource. I have read about the xml parsing implementation and found that all of them have entered the native layer. The native layer is implemented using c/c ++, which makes me look at the code ..

However, the various styles of ProgressBar can be set through the api. I haven't confirmed this yet. For details, refer to [Android instance] Android to set the style attribute in Java code-create a ProgressBar object using code.

If you have any experience in obtaining resources from this non-public system, please contact us.

 

Project code:

In the project, I constructed a style element, replaced it with attritutes, and the relationship between the style elements, including searching for theme android: attr:

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

 

Copyright. For more information, see the source:

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

 

Related Article

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.