【问题标题】:Android Error :You must supply layout_width attributeAndroid 错误:您必须提供 layout_width 属性
【发布时间】:2012-03-02 14:55:52
【问题描述】:

抱歉,因为我的标题与 * 上的许多其他人相似,但这些解决方案都没有解决我的问题。

我正在设计一个使用Relative Layout 的布局。在代码视图中设计后,当我切换到图形视图时,Eclipse 会注意到:

您必须提供一个 layout_width 属性。

您必须提供 layout_height 属性。

。当我运行这个程序时,会注意到 LogCat 中的错误

引起:java.lang.RuntimeException:二进制 XML 文件第 2 行:您 必须提供 layout_width 属性。

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/contact_name"
        android:text="Sam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/contact_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:text="39256493"
        android:layout_below="@+id/contact_name"
        android:layout_alignLeft="@+id/contact_name"
        android:layout_marginTop="5dp"/>        

</RelativeLayout>

【问题讨论】:

  • 那个 XML 看起来是正确的 - 这是你唯一的布局 XML 吗?检查其他以确保错误不在其中。
  • 我遇到了同样的错误。如果您的 xml 布局格式不正确,您可能会收到此错误。在我更正布局中的无效 xml 标签后,它起作用了。 hth!

标签: android xml layout android-relativelayout


【解决方案1】:

您的 xmlns 行应为 xmlns:android="http://schemas.android.com/apk/res/android"。 这可能会解决您的问题。

编辑:

http://developer.android.com/guide/topics/manifest/manifest-element.html引用谷歌:

xmlns:android 定义 Android 命名空间。

这个属性应该 始终设置为“http://schemas.android.com/apk/res/android”。

当您使用 XML 文件(如您正在使用的布局文件)时,您可以在同一文件中使用的属性由架构定义。

xmlns 是“XML 命名空间”的缩写。您在 XML 中定义了关键字“android:”的命名空间,这就是为什么您需要在开头使用“android:”声明所有属性,例如 android:layout_heightandroid:layout_width

命名空间应该指向一个有效的模式,该模式应该指向一个包含该模式的 URL。如果 URL 未指向有效架构,则无法识别您的 XML 属性,这就是您遇到的问题。

希望你能理解我的解释。

如果您想了解有关 XML 命名空间和模式的更多信息,我可以直接将您指向 W3Schools 和 Wikipedia:http://www.w3schools.com/xml/xml_namespaces.asphttp://en.wikipedia.org/wiki/XML_schema

【讨论】:

  • 哦。谢谢 :) 它对我有用。但是你能解释一下为什么吗,拜托:)我认为这只是一个URL,所以不重要:(
  • 当然!我编辑了我的帖子并解释了它。如果您需要进一步说明,请告诉我,我很乐意尽我所能提供帮助。
  • 哦。非常感谢 :D 我是 XML 的新手。我以前读过这些概念,但不仔细。有了你的解释和你的链接,我明白了更多:)
  • "android" 是您引用的架构的别名。如果您将“xmlns:android="schemas.android.com/apk/res/android”更改为“xmlns:droid="schemas.android.com/apk/res/android”,那么您可以在所有属性前面加上(例如)“droid:layout_width”。只要别名对于您的 .axml 是唯一的,那么一切都很好。为了节省时间,您可以只使用“a”作为别名,然后您可以将属性定义为“a:layout_width”......节省了大量的输入,并且谁不会厌倦输入“android:”
【解决方案2】:

这可能无关紧要,但命名空间标签引起了我的注意,因为 the usual one 是:

xmlns:android="http://schemas.android.com/apk/res/android"

【讨论】:

  • 哦。谢谢 :) 它对我有用。但是你能解释一下为什么吗,拜托:)我认为这只是一个 URL,所以不重要:(
  • URL 是 xml 架构文件的位置。模式文件为属于“命名空间”的给定数据集提供数据定义。它定义了 XML 文件的允许结构(例如,.axml 必须以某种布局元素开头......)以及每个元素允许的数据类型(例如,id 必须是字符串化的资源引用标识符)。通过将 "xmlns:android="schemas.android.com/apk/res/android" 引用添加到您的 .axml 中,您声明后面的 xml 必须符合位于该 url 的 shema。
【解决方案3】:

你应该换行:xmlns:android="http://schemas.android.com 到线:xmlns:android="http://schemas.android.com/apk/res/android

我遇到了这个错误,这很痛苦。但我无法解释为什么。

希望它对你有用:)

【讨论】:

  • 哦。谢谢 :) 它对我有用。但是你能解释一下为什么吗,拜托:)我认为这只是一个 URL,所以不重要:(
  • URL“schemas.android.com/apk/res/android”是架构文件的位置。模式文件定义了给定命名空间允许的数据类型和值。一个
【解决方案4】:

你能尝试做项目吗?如果这不起作用,请尝试使用 match_parent 更改相对布局的参数。

【讨论】: