Android布局xml中的注释

167

我想在布局XML文件中添加一些注释,应该如何操作?

12个回答

284

就像其他人所说,XML中的注释是这样的

<!-- this is a comment -->

请注意它们可以跨越多行

<!--
    This is a comment
    on multiple lines
-->

但是它们不能嵌套

<!-- This <!-- is a comment --> This is not -->

同时你不能在标签内使用它们。

<EditText <!--This is not valid--> android:layout_width="fill_parent" />

3
如果在注释中使用双破折号--,XML 解析器会报错,因此不能这样写注释:<!-- this -- causes a problem but this - does not -->。 - Martin Belcher - AtWrk
如果您正在使用Eclipse,您可以打开XML文件,在您想要添加注释的位置放置光标,然后从顶部菜单中选择Source -> Add Block Comment。此外,还可以使用"ctrl + shft + /"(即按住控制键和Shift键,然后按下正斜杠键)。注释代码将在光标中间创建,因此您只需开始输入即可。 - LeBeau
12
很不幸,你不能在标签内使用它们。 - linuxjava

44

世界范围内的网页联盟(W3C)实际上定义了注释接口。该定义表示:在起始符“<!--”和结束符“-->”之间的所有字符都构成注释内容,对注释的内容不进行词汇检查

有关更多详细信息,请访问developer.android.com网站。

因此,您可以在任何起始和结束标记之间简单地添加您的注释。在Eclipse IDE中,只需键入<!--即可自动完成注释。然后您可以在其中添加注释文本。

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".TicTacToe" >

 <!-- This is a comment -->

</LinearLayout>

特别提到“in between”的目的是因为您不能在标签内使用它。

例如:

<TextView 
    android:text="@string/game_title"
    <!-- This is a comment -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

是错误的,并且会导致以下错误。

 Element type "TextView" must be followed by either attribute specifications, ">" or "/>".

2
请注意:标签内不应有注释。这应该是选定的答案。 - Eslam Sameh Ahmed
1
向Android Studio团队提交了一份改进。如果我使用数据绑定并想在XML中注释一行我编写的某些数据绑定逻辑,我必须在其他地方添加注释,这对于可视性或注释所指向的部分并没有帮助。这不应该是无法实现的事情,并且应该启用供我们(开发人员)使用。 - Chapz

19

XML注释以<!--开头,以-->结尾。

例如:

<!-- This is a comment. -->

14

Ctrl+Shift+/ 可以注释代码。

<!--    
     <View
          android:layout_marginTop="@dimen/d10dp"
          android:id="@+id/view1"
          android:layout_below="@+id/tv_change_password"
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="#c0c0c0"/>-->

14

你有两种方法可以做到这一点

  1. "<!--"开头,然后以"-->"结尾

    例如 <!-- 我的评论在这里 -->

  2. 高亮你想要注释的部分,然后按CTRL + SHIFT + /


7
<!-- comment here -->

5

可以在标签内部使用注释

可以创建自定义属性,用于注释/文档目的。

在下面的示例中,定义了一个documentation:info属性,具有一个示例注释值:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:documentation="documentation.mycompany.com"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relLayoutID"
    documentation:info="This is an example comment" >

    <TextView
        documentation:purpose="Instructions label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to begin."
        android:id="@+id/tvMyLabel"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        documentation:info="Another example comment"
        documentation:translation_notes="This control should use the fewest characters possible, as space is limited"
        />

</RelativeLayout>

请注意,在这种情况下,documentation.mycompany.com只是新自定义XML命名空间(documentation)的定义,因此只是唯一的URI字符串 - 它可以是任何唯一的内容。右侧的documentation也可以是任何内容 - 这与定义和使用android: XML命名空间的方式相同。
使用此格式,可以创建任意数量的属性,例如documentation:infodocumentation:translation_notes等,以及一个描述值,格式与任何XML属性相同。
总之:
  • 在XML布局文件的根(顶级)XML元素中添加一个xmls:my_new_namespace属性。将其值设置为唯一的字符串
  • 在文件中的任何子XML元素下,使用新的命名空间和任何单词来定义注释标记,这些标记在编译时被忽略,例如<TextView my_new_namespace:my_new_doc_property="description" />

2
请注意,这些属性在构建过程中不会被丢弃,而是将存储在生成的 APK 内部。考虑使用特殊的 tools: 命名空间,它确实会被丢弃。(当回答发布时可能不存在,但此页面仍然会吸引新的观众。) - j__m
@j__m 这是一个很好的观点。我还没有研究过ProGuard是否可以自动删除这个,或者需要一些配置... - CJBS

5

点击

在Windows上按下ctrl+shift+/

在Mac上按下command + control+/

然后输入任何内容,所有内容都将显示在评论中。


4

如果你想在 Android Studio 中添加注释,只需按下:

Windows/Linux: Ctrl + /

Mac: Cmd + /

无论是在类似于 strings.xml 的 XML 文件中还是在类似于 MainActivity.java 的代码文件中,此方法均适用。


0

你也可以通过按下 Ctrl+Shift+/ 和 Shift+/ 来添加注释,一行注释只需要按下 Shift + /。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接