7

Typically I have to write layout code like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" />

I want to do something like this:

<LinearLayout xmlns="http://schemas.android.com/apk/res/android"
              layout_width="fill_parent" 
              layout_height="fill_parent" 
              orientation="vertical" >

But this code doesn't run properly. Why?

And second question: Why element namen are in CamelCase and attributes are in under_score?

2 Answers 2

8

XML default namespaces do not apply to attribute names. Hence, you always have to specify the namespace of an attribute, if it has one:

Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

So the real question is: Why did the Android designers define the element names without a namespace, putting only the attributes into the Android namespace?

As the document suggests, if the element names were in the Android namespace, then attribute names really wouldn't need their own namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

Now I see. It's really weird because in XAML and MXML all controls are in appropriate namespaces and also there are no stupid underscore prefixes. So Microsoft and Adobe had proved that it's possible to make an elegant XML-based GUI markup language and Google just didn't managed to do that.
2

But this code doesn't run properly. Why?

Because the build tools do not support it. Last I checked, any prefixed namespace should work (e.g., xmlns:a="http://schemas.android.com/apk/res/android"), but the default namespace has never worked.

If you wish, you can propose and contribute a patch. Along the way, you will be able to determine whether or not there is a philosophical reason for this, a technical reason, or if they just never got around to it.

Why element namen are in CamelCase and attributes are in under_score?

Element names are Java classes, which are typically in CamelCase. Attributes are not "in under_score" in general -- the layout_ prefix indicates a family of attributes that are requests from a View to its container. But, if you look at the attributes more carefully, you will see that most are camelCase, ignoring this prefix (e.g., android:textSize).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.