Property Resources vs. Android namespaces

Source: Internet
Author: User
<span id="Label3"></p><p><p>Update: for custom attribute resource, now not practical http://schemas.android.com/apk/res/<Packge name> form, Unified with</p></p> <pre><pre>"http://schemas.android.com/apk/res-auto"</pre></pre> <p><p><br></p></p> <p><p>The Android application encapsulates all of the static resources in the APK file and generates a resource ID based on the values of these resource filenames (not including the Extension) or the key Property. These IDs are defined as variables in the corresponding subclasses of the R class. For example, all image resources (resource files in the res/drawable Directory) will generate the corresponding variable in the r.drawable class, which is the file name of the image Resource. When using these resources, the system will know where to look for the appropriate resources as long as the corresponding variables in the R class are Referenced. The positioning of most resources is well understood, for example, "@string/hello" references the string resource Hello. "@drawable/icon" refers to an image resource file (possibly an image such as icon.png, icon.jpg, and so on). however, a reference to a class of resources may be less noticeable to most beginners, which is attribute Resources.</p></p> <p><p>To explain the property resource, let's look at the definition code for a <TextView> tag.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><textview android:layout_width= "wrap_content" android:layout_height= "wrap_content"/></pre></pre> <p><p></p></p> <p><p>With the android:layout_width and Android:layout_height attributes set in this code, we'll find that if you change android:layout_width to android:layout_width1, Must not have been compiled Through. If you do not add the Android namespace, you can set the properties of the <TextView> tag arbitrarily, as long as the attributes and attribute values conform to the XML specification, you can compile the pass. For example, the following <TextView> tag is set with the Test property and can be compiled correctly.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" test= "value" /></pre></pre> <p><p>From this point, it can be judged that the System's validation of the Android:layout_width property is independent of the XML itself. So what exactly is it about?</p></p> <p><p>In fact, android:layout_width to separate to see, first look at Layout_width. There is a system-level R.java file inside the system, and all system resource-generated IDs are defined in the R class-related subclasses in the File. In this r class, there is a attr subclass that defines all the properties in the system, the attribute name set by the XML tag, and the class R package is Android.</p></p> <p><p>We do not need to care about the R class code, because this R class is automatically generated and compiled when the Android source code is compiled, the Android source code and the Android SDK do not have the source code of this r class. however, the reader can easily find an Android project, choose the Android 4.2 development package (also can be other versions), it is easy to locate the R class and its attr subclass, 8-12 black box shown.</p></p> <p><p></p></p> <p><p>There are many member variables that we are already familiar with in the attr class, for example, layout_width, Layout_height is the two variables in the attr class. So what's the use of this property resource?</p></p> <p><p>When the system detects an XML tag attribute, if it can determine the location of an R class, it is assumed that the current XML Tag's property name must match the name of a variable in the R.attr class, otherwise the property will be considered to be incorrect. So how does the system determine the position of the R class?</p></p> <p><p>The answer, of course, is the Android namespace, which means that the Android namespace directly specifies the location of the R class embedded in the System. Here's A look at the definition of the Android Namespace.</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" ......> ... </RelativeLayout></pre></pre> <p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);"><p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);">As we can see, the value of the Android namespace is a string in the form of a url, but this URL has some rules. The common form of this URL is as Follows:<br></p></p> <pre><pre>Http://schemas.android.com/apk/res/<Package name></pre></pre> <p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);"><p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);">In other words, this URL has a prefix called "http://schemas.android.com/apk/res/", followed by the package name of the R class. With the definition of the previous Android namespace, the package name is ANDROID. As mentioned earlier, the system-embedded R-class package is Android. So with this Android namespace, the system can know the full name of the R class (package name + Class name), so it can be positioned directly.</p></p> <p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);"><p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);">Although the value of the Android namespace cannot be modified, the name of the Android namespace can be modified, for example, the following <TextView> tag settings are Legal.<br></p></p> <pre><pre><relativelayout xmlns:mobile= "http://schemas.android.com/apk/res/android" ......> <textview Mobil E:layout_width= "wrap_content" mobile:layout_height= "wrap_content"/> </RelativeLayout></pre></pre> <p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);"><p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);">Assuming that the package for the R class in the current application is www.mobile.com, and to reference the property resource in the R class, the namespace should be set as Follows.<br></p></p> <pre><pre><relativelayout xmlns:mobile= "http://schemas.android.com/apk/res/www.mobile.com" ......> ... </relativelayout ></pre></pre> <p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);"><p style="margin-top: 10px; padding: 0px; color: rgb(51, 51, 51); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; white-space: normal; background-color: rgb(248, 248, 248);">of course, property resources can not only restrict property names, but also constrain property values.</p></p><p><p>Property Resources vs. Android namespaces</p></p></span>

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.