Why can't I define android attributes in the default namespace?

I usually have to write layout code as follows:

<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 does not work correctly. Why?

And the second question: why is the namen element in CamelCase, and the attributes are in under_score?

+7
source share
2 answers

XML namespaces do not apply to attribute names . Therefore, you should always specify an attribute namespace if it has one:

Default namespace declarations do not apply directly to attribute names; interpretation without prefix attributes are determined by the element on which they appear.

So the real question is: why did Android designers define element names without a namespace by placing only attributes in the Android namespace?

As stated in the document, if the element names were in the Android namespace, then the attribute names really would not need their own namespace.

+6
source

But this code does not work correctly. Why?

Because build tools do not support it. The last thing I checked should contain any prefix namespace (for example, xmlns:a="http://schemas.android.com/apk/res/android" ), but the default namespace never worked.

If you want, you can suggest and make corrections. Along the way, you can determine if there is a philosophical reason for this, a technical reason, or if they simply never circumvented it.

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

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

+1
source

All Articles