声明命名空间的元素的命名空间?

时间:2014-06-18 14:11:42

标签: android xml

在XML中,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:padding="24dp"
       android:text="@string/question_text" />

</LinearLayout>

LinearLayoutTextView元素的命名空间是什么?

它是http://schemas.android.com/apk/res/android命名空间吗?或者它们根本不在任何命名空间中?

最后,do属性是否必须明确定义它们所属的命名空间(如果有的话)?

1 个答案:

答案 0 :(得分:2)

使用Android提供的小部件,您不需要使用完整的包声明只是裸类名称(Ex LinearLayout,TextView等)。但是,如果您创建自定义窗口小部件,则必须使用其基于包的声明(例如com.example.android.MyAwesomeWidget)。

只有根小部件需要Android XML命名空间(xmlns:android =“http://schemas.android.com/apk/res/android”),因为所有子元素都继承该命名空间。这意味着一旦在根元素中定义了命名空间,就不需要将它放在子元素中。

最后,属性必须显式使用名称空间前缀以避免混淆和冲突。例如:android:layout_width =“match_parent”有效,但layout_width =“match_parent”不是。