Android Layout xml 中的注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3484643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:25:16  来源:igfitidea点击:

Comments in Android Layout xml

androidxml

提问by user412317

I would like to enter some comments into the layout XML files, how would I do that?

我想在布局 XML 文件中输入一些注释,我该怎么做?

回答by Federico klez Culloca

As other said, the comment in XML are like this

正如其他人所说,XML 中的注释是这样的

<!-- this is a comment -->

Notice that they can span on multiple lines

请注意,它们可以跨越多行

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

But they cannot be nested

但它们不能嵌套

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

Also you cannot use them inside tags

你也不能在标签内使用它们

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

回答by Aniket Thakur

The World Wide Web Consortium (W3C) actually defined a comment interface. The definition says all the characters between the starting ' <!--' and ending '-->' form a part of comment content and no lexical check is done on the content of a comment.

万维网联盟(W3C)实际上定义了一个评论接口。定义说all the characters between the starting ' <!--' and ending '-->' form a part of comment content and no lexical check is done on the content of a comment

More details are available on developer.android.comsite.

developer.android.com网站上提供了更多详细信息。

So you can simply add your comment in between any starting and ending tag. In Eclipse IDE simply typing <!--would auto complete the comment for you. You can then add your comment text in between.

所以你可以简单地在任何开始和结束标签之间添加你的评论。在 Eclipse IDE 中,只需键入即可<!--自动完成注释。然后,您可以在两者之间添加评论文本。

For example:

例如:

<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>

Purpose of specifically mentioning in betweenis because you cannot use it inside a tag.

特别提到的目的in between是因为你不能在标签中使用它。

For example:

例如:

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

is wrong and will give following error

是错误的,会出现以下错误

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

回答by Dan Dyer

XML comments start with <!--and end with -->.

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

For example:

例如:

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

回答by eli

There are two ways you can do that

有两种方法可以做到

  1. Start Your comment with "<!--"then end your comment with "-->"

    Example <!-- my comment goes here -->

  2. Highlight the part you want to comment and press CTRL + SHIFT + /

  1. 开始您的评论,"<!--"然后结束您的评论“-->"

    例子 <!-- my comment goes here -->

  2. 突出显示要评论的部分,然后按CTRL + SHIFT + /

回答by Yogesh Sarvaiya

ctrl+shift+/You can comment the code.

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"/>-->

回答by Wesley Wiser

<!-- comment here -->

回答by CJBS

Comments INSIDE tags possible

可以使用 INSIDE 标签

It's possible to create custom attributes that can be used for commenting/documentation purposes.

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

In the example below, a documentation:infoattribute is defined, with an example comment value:

在下面的示例中,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>

Note that in this case, documentation.mycompany.comis just a definition for the new custom XML namespace (of documentation), and is thus just a unique URI string- it can be anything as long as it's unique. The documentationto the right of the xmlns:can also be anything - this works the same way that the android:XML namespace is defined and used.

请注意,在这种情况下,documentation.mycompany.com只是新自定义 XML 命名空间(of documentation)的定义,因此只是一个唯一的 URI 字符串- 只要它是唯一的,它就可以是任何东西。该documentation到右边xmlns:也可以是任何东西-这工作了以同样的方式android:XML命名空间定义和使用。

Using this format, any number of attributes can be created, such as documentation:info, documentation:translation_notesetc., along with a description value, the format being the same as any XML attribute.

使用这种格式,可以创建任意数量的属性,例如documentation:infodocumentation:translation_notes等,以及描述值,格式与任何 XML 属性相同。

In summary:

总之:

  • Add a xmls:my_new_namespaceattribute to the root (top-level) XML element in the XML layout file. Set its value to a unique string
  • Under any child XML element within the file, use the new namespace, and any word following to define comment tags that are ignored when compiled, e.g. <TextView my_new_namespace:my_new_doc_property="description" />
  • xmls:my_new_namespace向 XML 布局文件中的根(顶级)XML 元素添加属性。将其值设置为唯一字符串
  • 在文件中的任何子 XML 元素下,使用新的命名空间和后面的任何单词来定义编译时忽略的注释标签,例如 <TextView my_new_namespace:my_new_doc_property="description" />

回答by Nabin Khatiwada

If you want to comment in Android Studiosimply press:

如果您想发表评论,Android Studio只需按:

Ctrl+ /on Windows/Linux

Ctrl+/在 Windows/Linux 上

Cmd+ /on Mac.

Cmd+/在 Mac 上。

This works in XML files such as strings.xmlas well as in code files like MainActivity.java.

这部作品在XML文件,例如strings.xml,以及像代码文件MainActivity.java

回答by venu46

click the

点击

ctrl+shift+/

ctrl+shift+/

and write anything you and evrything will be in comments

写下任何你和一切都会在评论中的东西

回答by venu46

you can also add comment by pressing Ctrl+shift+/ and shift+ / for one line.

您还可以通过按 Ctrl+shift+/ 和 shift+ / 为一行添加注释。