【问题标题】:Commenting in activity_main.xml在activity_main.xml 中评论
【发布时间】:2013-05-30 01:27:31
【问题描述】:

我是 Android 新手。我了解 XML 中的注释与 HTML 中的注释相同,使用

<!-- comment here -->

表格。我想在我的 activity_main.xml 配置文件中为 Android 项目编写一些 cmets,但它给了我错误。值得注意的是,我使用的是 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”必须后跟属性规范,“>”或“/>”。

一个人回答说评论不能分解标签,这是我打算做的。我以为我以前在 HTML 中做过,所以我假设 XML 遵守相同的规则。我想不会,或者我只需要复习一下我的 XML。

【问题讨论】:

  • 而且,activity_main.xml 内容是...
  • 您遇到了哪个错误?你可以在这里发帖吗?
  • 什么是错误信息?我的 XML 文件中充满了这样的 cmets,我没有收到任何错误。
  • 有时,如果您使用 Eclipse,编译器会在错误的行中给出错误。你提出的评论格式对我来说是正确的,让我们检查一下xml的另一个地方,看看一个标签是否错误!请张贴xml!

标签: android xml eclipse comments


【解决方案1】:

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

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

你不能评论layout_width,你的评论必须在view标签之外。

【讨论】:

    【解决方案2】:

    在 XML 中注释有两件事是被禁止的:

    • double -- 在 cmets 内
    • 标签内的cmets

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

    <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 中 cmets 的更多信息:http://android4beginners.com/2013/07/appendix-d-comments-in-xml-and-java-its-crucial-to-create-a-documentation-of-android-app/

    【讨论】:

      【解决方案3】:

      它给了你错误,因为你在第二行本身关闭了你的 EditText 标记。

      那是因为注释掉一些代码,你会使用&lt;!-- --&gt;,所以 ">" 已经被使用了。从下一行开始,它没有开始的“

      例如,

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

      如果你注释掉第二行,即layout_width,它会被认为好像你正在关闭TextView标签,并且下一行以后它不会有任何开始的“

      【讨论】:

        猜你喜欢
        • 2014-12-11
        • 2011-04-15
        • 2021-09-09
        • 2011-12-18
        • 2014-07-06
        • 1970-01-01
        • 2022-11-18
        • 1970-01-01
        • 2013-09-21
        相关资源
        最近更新 更多