Hit*_*iya 137 xml layout android xml-namespaces android-activity
为什么在xml布局文件中需要这一行?
xmlns:android="http://schemas.android.com/apk/res/android"
Run Code Online (Sandbox Code Playgroud)
Nit*_*G42 114
在XML中,xmlns声明了一个命名空间.事实上,当你这样做时:
<LinearLayout android:id>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
而不是调用android:id,xml将使用http://schemas.android.com/apk/res/android:id是唯一的.通常,此页面不存在(它是URI,而不是URL),但有时它是解释使用的命名空间的URL.
命名空间与Java应用程序中的包名几乎相同.
这是一个解释.
统一资源标识符(URI)
统一资源标识符(URI)是标识Internet资源的字符串.
最常见的URI是标识Internet域地址的统一资源定位符(URL).另一种不常见的URI类型是通用资源名称(URN).
在我们的示例中,我们将仅使用URL.
Dev*_*ath 34
要理解为什么xmlns:android=“http://schemas.android.com/apk/res/android”必须是布局xml文件中的第一个我们将使用示例来理解组件
Sample::
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container" >
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
例如:http://schemas.android.com/apk/res/android:id这里是URI
xmlns:android描述了android命名空间.textview小部件,与android相比具有不同的功能textview,android命名空间有助于区分我们的自定义textview小部件和android
textview小部件xmlns:android 定义Android命名空间.此属性应始终设置为"http://schemas.android.com/apk/res/android".
请参阅http://developer.android.com/guide/topics/manifest/manifest-element.html
小智 6
这只是XML名称空间声明。我们使用此名称空间来指定下面列出的属性属于Android。因此,它们以“ android: ” 开头
您实际上可以创建自己的自定义属性。因此,为了防止在两个属性被命名为相同事物但行为不同的情况下发生名称冲突,我们添加前缀“ android: ”以表示这些是Android属性。
因此,此名称空间声明必须包含在XML文件的根视图的开始标记中。
用外行的话来说:
如果没有 xmlns:android=" http://schemas.android.com/apk/res/android " android 相关标签将不会在我们布局的 xml 文档中被识别。