在activity_main.xml中发表评论

时间:2013-06-03 19:34:50

标签: android xml eclipse comments

我是Android新手。我理解使用

在XML中进行注释与在HTML中进行注释一样
<!-- comment here -->

形式。我想在我的activity_main.xml配置文件中为Android项目写一些注释,但它给了我错误。值得注意的是我正在使用Eclipse,但目前我正在编辑XML文件而不是图形化,因为我宁愿强迫自己理解属性。我试图评论冲突的属性,但它给了我红色。

Eclipse有没有办法让我评论那个特定的XML文件?

对于那些问的人,这是一个例子:

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" 
    android:layout_weight="1" />

说我想简单地说出第二行。我想注释掉layout_width标签,因为我后来使用layout_weight。当我试着评论它时,我得到:

  

元素类型“EditText”必须后跟属性规范,“&gt;”或“/&gt;”。

有人回复说评论不能打破标签,这是我打算做的。我以为我之前用HTML做过,所以我认为XML遵循相同的规则。我猜不是,或者我只需要刷新我的XML。

3 个答案:

答案 0 :(得分:1)

在xml上注释属性的问题是你不能让注释破坏xml标记。所以如果你有:

<View 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
/>

您无法对layout_width发表评论,您的评论必须位于视图标记之外。

答案 1 :(得分:0)

在XML注释方面有两件事是禁止的:

  • double - 内部评论
  • 标签内的评论

所以这段代码会产生错误。

<EditText android:id="@+id/edit_message"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:hint="@string/edit_message" 
  <!-- That would use all empty space -->
  android:layout_weight="1" />

这也是:

<!-- This is for -- your name -->
<EditText android:id="@+id/edit_message"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:hint="@string/edit_message"       
  android:layout_weight="1" />

您可以在Android中阅读有关评论的更多信息:http://android4beginners.com/2013/07/appendix-d-comments-in-xml-and-java-its-crucial-to-create-a-documentation-of-android-app/

答案 2 :(得分:-1)

它正在为您提供错误,因为您正在关闭第二行中的EditText标记。

这是因为为了评论一些代码,你会使用<!-- -->,所以“&gt;”被拿了。从下一行开始,它没有起始“&lt;”。

例如,

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

如果你注释掉第二行,即layout_width,它将被视为关闭TextView标记,然后下一行它将没有任何开始“&lt;”。