我想在布局XML文件中添加一些注释,应该如何操作?
就像其他人所说,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" />
世界范围内的网页联盟(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 "/>".
XML注释以<!--
开头,以-->
结尾。
例如:
<!-- This is a comment. -->
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"/>-->
你有两种方法可以做到这一点
以"<!--"
开头,然后以"-->"
结尾
例如 <!-- 我的评论在这里 -->
高亮你想要注释的部分,然后按CTRL + SHIFT + /
<!-- comment here -->
可以创建自定义属性,用于注释/文档目的。
在下面的示例中,定义了一个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:info
、documentation:translation_notes
等,以及一个描述值,格式与任何XML属性相同。xmls:my_new_namespace
属性。将其值设置为唯一的字符串<TextView my_new_namespace:my_new_doc_property="description" />
tools:
命名空间,它确实会被丢弃。(当回答发布时可能不存在,但此页面仍然会吸引新的观众。) - j__m点击
在Windows上按下ctrl+shift+/
在Mac上按下command + control+/
然后输入任何内容,所有内容都将显示在评论中。
如果你想在 Android Studio
中添加注释,只需按下:
Windows/Linux: Ctrl + /
Mac: Cmd + /
无论是在类似于 strings.xml
的 XML 文件中还是在类似于 MainActivity.java
的代码文件中,此方法均适用。
你也可以通过按下 Ctrl+Shift+/ 和 Shift+/ 来添加注释,一行注释只需要按下 Shift + /。