Part of the reason for my difficulty is that the Android documentation assumes you know all the ins and outs of XML. If, like me, you think of XML as just neater HTML, then you'll need to learn about XML Namespaces.
A Crash Course on XML Namespaces
Anyone can make up their own XML tags and attributes, which leads to the possibility of conflicting tag and attribute names. (In the case of Android, they've created dozens of built-in XML attributes, and now the use can create their own. Who knows if you're going to accidentally reuse an attribute the Google folks have already used.)
The solution is the Namespace concept. You simply add a prefix to each XML tag name. We've all seen one example: you don't get far in Android development without learning that Google's prefix is "android" as in android.layout_width, android.text etc.
So how do we ensure that the prefix is unique? That's where that mysterious "xmlns" attribute at the start of android resource files comes in. To refresh your memory, it says this:
xmlns:android="http://schemas.android.com/apk/res/android"
The purpose is to link the prefix to a URL. For instance, if I wanted to use the prefix, "tomato" I'd add the attribute:
xmlns:tomato="http://oksmartphone.blogspot.com"
This attribute doesn't have to go at the start of the file - the prefix will be usable in the tag with the xmlns attribute, and all those contained within. So putting it in the outermost container, along with the android prefix, will ensure it applies to the entire file. After that, If I want to have an attribute called, say, "depth", I could add it with the tomato prefix
<com.blogspot.oksmartphone.JasonView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tomato:depth="10"/>
android:layout_height="wrap_content"
tomato:depth="10"/>
An important point to realise is that the value of this attribute just has to be a unique string - there doesn't have to be any file relating to your attributes at that web page. In fact, it doesn't have to be a URL at all. I could have used:
xmlns:tomato="http://www.totally-fake-site.com"
or xmlns:tomato="blah blah blah I hope this is unique"
Using a URL just a custom that ensures that the string is unique.
Accessing Custom Attributes
Accessing attributes from a View class isn't hard. When a View is created as part of an XML layout file, the constructor passes an AttributeSet object to the constructor.
The AttributeSet has lots of methods for accessing attributes (such as getAttributeValue.) Each one has parameters for "namespace" and "attribute." Note that the "namespace" refers to the unique string from the xmlns attribute, not the prefix associated with it (from my first example, I'd use "http://oksmartphone.blogspot.com/" and not "tomato". For the attribute, leave off the prefix. Again using my above example, I'd call:
attrs.getAttributeIntValue("http://oksmartphone.blogspot.com/", "depth", 0);
Assuming I wanted the default value (if the attribute is missing) to be zero.
For a better explanation of XML namespaces, try W3 Schools' page: http://www.w3schools.com/xml/xml_namespaces.asp
No comments:
Post a Comment