Android Layout xml中的评论

use*_*317 141 xml android

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

Fed*_*oca 248

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

<!-- this is a comment -->
Run Code Online (Sandbox Code Playgroud)

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

<!--
    This is a comment
    on multiple lines
-->
Run Code Online (Sandbox Code Playgroud)

但它们不能嵌套

<!-- This <!-- is a comment --> This is not -->
Run Code Online (Sandbox Code Playgroud)

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

<EditText <!--This is not valid--> android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)

  • >此外,你不能在标签内使用它们.非常不幸. (6认同)
  • 此外,您不能在评论中使用双破折号,或者XML解析器会抱怨<! - 这会导致问题但是 - 不 - > (3认同)

Ani*_*kur 38

万维网联盟(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.

有关详细信息,请访问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>
Run Code Online (Sandbox Code Playgroud)

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

例如:

<TextView 
    android:text="@string/game_title"
    <!-- This is a comment -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

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

 Element type "TextView" must be followed by either attribute specifications, ">" or "/>".
Run Code Online (Sandbox Code Playgroud)

  • 注意:标签内没有注释。这应该是选定的答案 (2认同)

Dan*_*yer 19

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

例如:

<!-- This is a comment. -->
Run Code Online (Sandbox Code Playgroud)


eli*_*eli 9

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

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

    <!-- my comment goes here -->

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


Yog*_*iya 6

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"/>-->
Run Code Online (Sandbox Code Playgroud)


CJB*_*JBS 5

可以使用 INSIDE 标签

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

在下面的示例中,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>
Run Code Online (Sandbox Code Playgroud)

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

使用此格式,可以创建任何数量的属性,诸如documentation:infodocumentation:translation_notes等,具有描述值一起,该格式是相同的任何XML属性。

总之:

  • xmls:my_new_namespace向 XML 布局文件中的根(顶级)XML 元素添加属性。将其值设置为唯一字符串
  • 在文件中的任何子 XML 元素下,使用新的命名空间和后面的任何单词来定义编译时忽略的注释标签,例如 <TextView my_new_namespace:my_new_doc_property="description" />

  • 请注意,这些属性在构建过程中不会被丢弃,而是存储在生成的 APK 中。考虑使用特殊的“tools:”命名空间,它确实会被丢弃。(发布此答案时它可能不存在,但此页面继续吸引新的查看者。) (2认同)

ven*_*u46 5

点击

Windows 上的 ctrl+shift+/

Mac 上的命令 + 控制 +/

并在评论中写下您和所有内容